1
0
mirror of https://github.com/zenorogue/hyperrogue.git synced 2024-12-27 18:40:35 +00:00
hyperrogue/basegraph.cpp

1350 lines
36 KiB
C++
Raw Normal View History

// Hyperbolic Rogue -- basic graphics
// Copyright (C) 2011-2018 Zeno Rogue, see 'hyper.cpp' for details
namespace hr {
display_data default_display;
display_data *current_display = &default_display;
2017-07-24 22:21:36 +00:00
unsigned backcolor = 0;
unsigned bordcolor = 0;
unsigned forecolor = 0xFFFFFF;
2017-07-10 18:47:38 +00:00
2017-07-22 23:33:27 +00:00
int utfsize(char c) {
unsigned char cu = c;
if(cu < 128) return 1;
if(cu < 224) return 2;
if(cu < 0xF0) return 3;
return 4;
}
2017-07-10 18:47:38 +00:00
int get_sightrange() { return getDistLimit() + sightrange_bonus; }
2018-04-11 11:16:40 +00:00
int get_sightrange_ambush() { return max(get_sightrange(), ambush_distance); }
2018-02-03 12:41:49 +00:00
namespace stereo {
eStereo mode;
ld ipd;
ld lr_eyewidth, anaglyph_eyewidth;
ld fov, tanfov;
2019-04-29 09:16:37 +00:00
GLfloat scrdist;
2018-02-03 12:41:49 +00:00
}
bool display_data::in_anaglyph() { return vid.stereo_mode == sAnaglyph; }
bool display_data::stereo_active() { return vid.stereo_mode != sOFF; }
2018-02-03 12:41:49 +00:00
ld display_data::eyewidth() {
switch(vid.stereo_mode) {
case sAnaglyph:
return vid.anaglyph_eyewidth;
case sLR:
return vid.lr_eyewidth;
2018-02-03 12:41:49 +00:00
default:
return 0;
}
}
2017-07-22 23:33:27 +00:00
bool eqs(const char* x, const char* y) {
return *y? *x==*y?eqs(x+1,y+1):false:true;
}
int getnext(const char* s, int& i) {
int siz = utfsize(s[i]);
// if(fontdeb) printf("s=%s i=%d siz=%d\n", s, i, siz);
if(siz == 1) return s[i++];
for(int k=0; k<NUMEXTRA; k++)
if(eqs(s+i, natchars[k])) {
i += siz; return 128+k;
}
2018-02-11 20:39:08 +00:00
printf("Unknown character in: '%s' at position %d\n", s, i);
2017-07-22 23:33:27 +00:00
i ++; return '?';
}
#if CAP_SDLTTF
2017-07-10 18:47:38 +00:00
TTF_Font *font[256];
#endif
2017-07-22 23:33:27 +00:00
#if CAP_SDL
SDL_Surface *s, *s_screen;
2017-07-22 23:33:27 +00:00
color_t qpixel_pixel_outside;
2017-07-10 18:47:38 +00:00
color_t& qpixel(SDL_Surface *surf, int x, int y) {
2017-12-22 20:24:08 +00:00
if(x<0 || y<0 || x >= surf->w || y >= surf->h) return qpixel_pixel_outside;
2017-07-10 18:47:38 +00:00
char *p = (char*) surf->pixels;
p += y * surf->pitch;
color_t *pi = (color_t*) (p);
2017-07-10 18:47:38 +00:00
return pi[x];
}
#endif
2017-07-22 23:33:27 +00:00
#if CAP_SDLTTF
2017-12-14 01:50:28 +00:00
string fontpath = ISWEB ? "sans-serif" : HYPERPATH "DejaVuSans-Bold.ttf";
2017-07-10 18:47:38 +00:00
void loadfont(int siz) {
if(!font[siz]) {
2017-12-14 01:50:28 +00:00
font[siz] = TTF_OpenFont(fontpath.c_str(), siz);
2017-07-10 18:47:38 +00:00
// Destination set by ./configure (in the GitHub repository)
#ifdef FONTDESTDIR
if (font[siz] == NULL) {
font[siz] = TTF_OpenFont(FONTDESTDIR, siz);
}
#endif
if (font[siz] == NULL) {
2017-12-14 01:50:28 +00:00
printf("error: Font file not found: %s\n", fontpath.c_str());
2017-07-10 18:47:38 +00:00
exit(1);
}
}
}
#endif
2017-07-22 23:33:27 +00:00
#if !ISFAKEMOBILE && !ISANDROID & !ISIOS
2017-07-10 18:47:38 +00:00
int textwidth(int siz, const string &str) {
2018-06-22 12:47:24 +00:00
if(isize(str) == 0) return 0;
2017-07-10 18:47:38 +00:00
2017-07-22 23:33:27 +00:00
#if CAP_SDLTTF
2017-07-10 18:47:38 +00:00
loadfont(siz);
int w, h;
TTF_SizeUTF8(font[siz], str.c_str(), &w, &h);
2018-06-22 12:47:24 +00:00
// printf("width = %d [%d]\n", w, isize(str));
2017-07-10 18:47:38 +00:00
return w;
2017-07-22 23:33:27 +00:00
#elif CAP_GL
return gl_width(siz, str.c_str());
#else
return 0;
2017-07-10 18:47:38 +00:00
#endif
}
#endif
2017-07-22 23:33:27 +00:00
#if ISIOS
2017-07-10 18:47:38 +00:00
int textwidth(int siz, const string &str) {
return mainfont->getSize(str, siz / 36.0).width;
}
#endif
int darkenedby(int c, int lev) {
for(int i=0; i<lev; i++)
c = ((c & 0xFEFEFE) >> 1);
return c;
}
2017-12-01 23:19:34 +00:00
bool fading = false;
ld fadeout = 1;
color_t darkened(color_t c) {
2017-07-16 21:00:55 +00:00
if(inmirrorcount&1)
c = gradient(c, winf[waMirror].color, 0, 0.5, 1);
else if(inmirrorcount)
c = gradient(c, winf[waCloud].color, 0, 0.5, 1);
2017-12-01 23:19:34 +00:00
if(fading) c = gradient(backcolor, c, 0, fadeout, 1);
if(vid.desaturate) {
ld luminance = 0.2125 * part(c,2) + 0.7154 * part(c,1) + 0.0721 * part(c, 0);
c = gradient(c, int(luminance+.5) * 0x10101, 0, vid.desaturate, 100);
}
2017-07-10 18:47:38 +00:00
for(int i=0; i<darken; i++)
c = ((c & 0xFEFEFE) >> 1) + ((backcolor & 0xFEFEFE) >> 1);
return c;
}
color_t darkena3(color_t c, int lev, int a) {
2017-07-10 18:47:38 +00:00
return (darkenedby(c, lev) << 8) + a;
}
color_t darkena(color_t c, int lev, int a) {
return darkena3(c, lev, DIM == 3 ? 255 : a);
}
2017-07-22 23:33:27 +00:00
#if !CAP_GL
2017-07-10 18:47:38 +00:00
void setcameraangle(bool b) { }
2017-07-22 23:33:27 +00:00
#else
2017-07-10 18:47:38 +00:00
2018-11-17 16:59:57 +00:00
bool shaderside_projection;
void start_projection(int ed, bool perspective) {
2019-04-29 01:37:27 +00:00
glhr::use_projection();
2018-02-08 23:29:20 +00:00
glhr::new_projection();
2018-11-17 16:59:57 +00:00
shaderside_projection = perspective;
auto cd = current_display;
if(ed && vid.stereo_mode == sLR) {
glhr::projection_multiply(glhr::translate(ed, 0, 0));
glhr::projection_multiply(glhr::scale(2, 1, 1));
}
2018-11-17 23:35:50 +00:00
ld tx = (cd->xcenter-cd->xtop)*2./cd->xsize - 1;
ld ty = (cd->ycenter-cd->ytop)*2./cd->ysize - 1;
glhr::projection_multiply(glhr::translate(tx, -ty, 0));
}
2017-07-10 18:47:38 +00:00
void eyewidth_translate(int ed) {
if(ed) glhr::projection_multiply(glhr::translate(-ed * current_display->eyewidth(), 0, 0));
2018-02-03 12:41:49 +00:00
}
glhr::glmatrix model_orientation_gl() {
glhr::glmatrix s = glhr::id;
for(int a=0; a<DIM; a++)
conformal::apply_orientation(s[a][1], s[a][0]);
if(DIM == 3) for(int a=0; a<DIM; a++)
conformal::apply_orientation_yz(s[a][2], s[a][1]);
return s;
}
2019-04-23 13:03:17 +00:00
tuple<int, eModel, display_data*, int> last_projection;
bool new_projection_needed;
void display_data::set_all(int ed) {
auto t = this;
auto current_projection = tie(ed, pmodel, t, current_rbuffer);
if(new_projection_needed || last_projection != current_projection) {
last_projection = current_projection;
set_projection(ed);
set_mask(ed);
set_viewport(ed);
new_projection_needed = false;
}
}
void display_data::set_projection(int ed) {
2019-05-12 23:57:40 +00:00
DEBBI(DF_GRAPH, ("current_display->set_projection"));
2018-02-03 12:41:49 +00:00
bool pers3 = false;
2019-05-12 12:35:14 +00:00
bool apply_models = !among(pmodel, mdUnchanged, mdFlatten, mdRug);
2019-02-25 16:21:53 +00:00
2018-11-17 16:59:57 +00:00
shaderside_projection = false;
glhr::new_shader_projection = glhr::shader_projection::standard;
if(vid.consider_shader_projection) {
2019-02-22 20:01:01 +00:00
if(pmodel == mdDisk && !spherespecial && !(hyperbolic && vid.alpha <= -1) && DIM == 2)
shaderside_projection = true;
if(pmodel == mdDisk && !spherespecial && !(hyperbolic && vid.alpha <= -1) && DIM == 3 && apply_models)
shaderside_projection = true, glhr::new_shader_projection = glhr::shader_projection::ball;
if(pmodel == mdBand && hyperbolic && apply_models)
shaderside_projection = true, glhr::new_shader_projection = (DIM == 2 ? glhr::shader_projection::band : glhr::shader_projection::band3);
if(pmodel == mdHalfplane && hyperbolic && apply_models && DIM == 2)
shaderside_projection = true, glhr::new_shader_projection = glhr::shader_projection::halfplane;
if(pmodel == mdHalfplane && hyperbolic && apply_models && DIM == 3 && vid.alpha == 1)
shaderside_projection = true, glhr::new_shader_projection = glhr::shader_projection::halfplane3;
if(DIM == 3 && hyperbolic && apply_models && pmodel == mdPerspective)
shaderside_projection = true, glhr::new_shader_projection = glhr::shader_projection::standardH3, pers3 = true;
if(DIM == 3 && euclid && apply_models && pmodel == mdPerspective)
shaderside_projection = true, glhr::new_shader_projection = glhr::shader_projection::standardR3, pers3 = true;
if(DIM == 3 && sphere && apply_models && pmodel == mdPerspective) {
shaderside_projection = true; pers3 = true;
2019-02-25 12:17:10 +00:00
if(spherephase == 0) glhr::new_shader_projection = glhr::shader_projection::standardS30;
if(spherephase == 1) glhr::new_shader_projection = glhr::shader_projection::standardS31;
if(spherephase == 2) glhr::new_shader_projection = glhr::shader_projection::standardS32;
if(spherephase == 3) glhr::new_shader_projection = glhr::shader_projection::standardS33;
}
}
2019-05-12 12:35:14 +00:00
if(pmodel == mdFlatten) shaderside_projection = true, glhr::new_shader_projection = glhr::shader_projection::flatten;
2018-11-17 16:59:57 +00:00
start_projection(ed, shaderside_projection);
2019-04-23 13:03:17 +00:00
if(pmodel == mdRug) return;
2017-07-10 18:47:38 +00:00
2018-11-17 23:35:50 +00:00
auto cd = current_display;
2019-05-12 12:35:14 +00:00
if(!shaderside_projection || glhr::new_shader_projection == glhr::shader_projection::flatten) {
2019-03-21 18:11:28 +00:00
if(DIM == 3 && apply_models) {
glhr::projection_multiply(glhr::ortho(cd->xsize/2, -cd->ysize/2, 1));
glhr::id_modelview();
}
else {
glhr::projection_multiply(glhr::ortho(cd->xsize/2, -cd->ysize/2, abs(current_display->scrdist) + 30000));
if(ed) {
glhr::glmatrix m = glhr::id;
m[2][0] -= ed;
glhr::projection_multiply(m);
}
glhr::id_modelview();
}
2018-02-03 12:41:49 +00:00
}
else {
2019-02-21 17:46:53 +00:00
if(hyperbolic && vid.alpha > -1 && DIM == 2) {
// Because of the transformation from H3 to the Minkowski hyperboloid,
// points with negative Z can be generated in some 3D settings.
// This happens for points below the camera, but above the plane.
// These points should still be viewed, though, so we disable the
// depth clipping
glhr::projection_multiply(glhr::scale(1,1,0));
}
eyewidth_translate(ed);
if(pers3) {
2019-03-09 15:20:06 +00:00
glhr::projection_multiply(glhr::frustum(current_display->tanfov, current_display->tanfov * cd->ysize / cd->xsize));
2019-02-28 02:40:14 +00:00
glhr::projection_multiply(glhr::scale(1, -1, -1));
}
else if(DIM == 3) {
2019-03-20 23:25:38 +00:00
glhr::glmatrix M = glhr::ortho(cd->xsize/current_display->radius/2, -cd->ysize/current_display->radius/2, 1);
2019-03-23 15:22:46 +00:00
using conformal::clip_max;
using conformal::clip_min;
2019-03-20 23:25:38 +00:00
M[2][2] = 2 / (clip_max - clip_min);
M[3][2] = (clip_min + clip_max) / (clip_max - clip_min);
glhr::projection_multiply(M);
}
2019-02-28 02:40:14 +00:00
else {
glhr::projection_multiply(glhr::frustum(cd->xsize / cd->ysize, 1));
GLfloat sc = current_display->radius / (cd->ysize/2.);
glhr::projection_multiply(glhr::scale(sc, -sc, -1));
}
2018-02-03 12:41:49 +00:00
2017-07-10 18:47:38 +00:00
if(ed) glhr::projection_multiply(glhr::translate(vid.ipd * ed/2, 0, 0));
2018-02-03 12:41:49 +00:00
2019-02-22 20:01:01 +00:00
if(pers3) {
2019-02-25 16:21:53 +00:00
glhr::fog_max(1/sightranges[geometry]);
2019-02-22 20:01:01 +00:00
}
if(glhr::new_shader_projection == glhr::shader_projection::ball)
glhr::set_ualpha(vid.alpha);
if(among(glhr::new_shader_projection, glhr::shader_projection::band, glhr::shader_projection::band3)) {
glhr::projection_multiply(model_orientation_gl());
glhr::projection_multiply(glhr::scale(2 / M_PI, 2 / M_PI, DIM == 3 ? 2/M_PI : 1));
}
if(among(glhr::new_shader_projection, glhr::shader_projection::halfplane, glhr::shader_projection::halfplane3)) {
glhr::projection_multiply(model_orientation_gl());
glhr::projection_multiply(glhr::translate(0, 1, 0));
glhr::projection_multiply(glhr::scale(-1, 1, 1));
2019-03-21 18:11:47 +00:00
glhr::projection_multiply(glhr::scale(conformal::halfplane_scale, conformal::halfplane_scale, DIM == 3 ? conformal::halfplane_scale : 1));
glhr::projection_multiply(glhr::translate(0, 0.5, 0));
}
2017-07-10 18:47:38 +00:00
}
2019-05-12 12:35:14 +00:00
if(vid.camera_angle && !among(pmodel, mdUnchanged, mdFlatten, mdRug)) {
2019-04-23 13:03:17 +00:00
ld cam = vid.camera_angle * degree;
GLfloat cc = cos(cam);
GLfloat ss = sin(cam);
GLfloat yzspin[16] = {
1, 0, 0, 0,
0, cc, ss, 0,
0, -ss, cc, 0,
0, 0, 0, 1
};
glhr::projection_multiply(glhr::as_glmatrix(yzspin));
}
2017-07-10 18:47:38 +00:00
}
void display_data::set_mask(int ed) {
if(ed == 0 || vid.stereo_mode != sAnaglyph) {
2017-07-10 18:47:38 +00:00
glColorMask( GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE );
}
else if(ed == 1) {
glColorMask( GL_TRUE,GL_FALSE,GL_FALSE,GL_TRUE );
}
else if(ed == -1) {
glColorMask( GL_FALSE,GL_TRUE,GL_TRUE,GL_TRUE );
}
}
void display_data::set_viewport(int ed) {
2018-11-20 18:01:10 +00:00
ld xtop = current_display->xtop;
ld ytop = current_display->ytop;
ld xsize = current_display->xsize;
ld ysize = current_display->ysize;
if(ed == 0 || vid.stereo_mode != sLR) ;
else if(ed == 1) xsize /= 2;
2018-11-20 18:01:10 +00:00
else if(ed == -1) xsize /= 2, xtop += xsize;
2018-11-20 18:01:10 +00:00
glViewport(xtop, ytop, xsize, ysize);
}
2018-02-03 12:41:49 +00:00
bool model_needs_depth() {
2019-02-21 17:46:53 +00:00
return DIM == 3 || pmodel == mdBall;
}
void setGLProjection(color_t col) {
2019-05-12 23:57:40 +00:00
DEBBI(DF_GRAPH, ("setGLProjection"));
2018-02-11 01:19:49 +00:00
GLERR("pre_setGLProjection");
2017-07-10 18:47:38 +00:00
2019-02-06 17:39:53 +00:00
glClearColor(part(col, 2) / 255.0, part(col, 1) / 255.0, part(col, 0) / 255.0, 1);
2017-07-10 18:47:38 +00:00
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2018-02-11 01:19:49 +00:00
GLERR("setGLProjection #1");
2017-07-10 18:47:38 +00:00
glEnable(GL_BLEND);
2018-02-11 18:08:17 +00:00
#ifndef GLES_ONLY
2017-07-10 18:47:38 +00:00
if(vid.antialias & AA_LINES) {
glEnable(GL_LINE_SMOOTH);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
}
else glDisable(GL_LINE_SMOOTH);
2018-02-11 01:19:49 +00:00
#endif
2017-07-10 18:47:38 +00:00
glLineWidth(vid.linewidth);
2017-07-22 23:33:27 +00:00
2018-02-11 01:19:49 +00:00
GLERR("setGLProjection #2");
#ifndef GLES_ONLY
2017-07-10 18:47:38 +00:00
if(vid.antialias & AA_POLY) {
glEnable(GL_POLYGON_SMOOTH);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
}
else glDisable(GL_POLYGON_SMOOTH);
2017-07-22 23:33:27 +00:00
#endif
2017-07-10 18:47:38 +00:00
2018-02-11 01:19:49 +00:00
GLERR("setGLProjection #3");
2017-07-10 18:47:38 +00:00
//glLineWidth(1.0f);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
if(model_needs_depth()) {
2017-07-10 18:47:38 +00:00
#ifdef GL_ES
glClearDepthf(1.0f);
#else
glClearDepth(1.0f);
#endif
glDepthFunc(GL_LEQUAL);
glhr::set_depthtest(true);
2017-07-10 18:47:38 +00:00
}
else
glhr::set_depthtest(false);
2017-07-10 18:47:38 +00:00
2018-02-11 01:19:49 +00:00
GLERR("setGLProjection");
2019-04-23 13:03:17 +00:00
reset_projection();
2017-07-10 18:47:38 +00:00
}
inline int next_p2 (int a )
{
int rval=1;
// rval<<=1 Is A Prettier Way Of Writing rval*=2;
while(rval<a) rval<<=1;
return rval;
}
2017-07-22 23:33:27 +00:00
#if CAP_GLFONT
2017-07-10 18:47:38 +00:00
2018-09-04 21:27:51 +00:00
#define CHARS (128+NUMEXTRA)
2017-07-10 18:47:38 +00:00
struct glfont_t {
2018-09-04 21:27:51 +00:00
GLuint texture; // Holds The Texture Id
2017-07-10 18:47:38 +00:00
//GLuint list_base; // Holds The First Display List ID
2018-09-04 21:27:51 +00:00
int widths[CHARS];
int heights[CHARS];
float tx0[CHARS], tx1[CHARS], ty0[CHARS], ty1[CHARS];
2017-07-10 18:47:38 +00:00
};
glfont_t *glfont[256];
2018-09-04 21:27:51 +00:00
typedef Uint16 texturepixel;
#define FONTTEXTURESIZE 2048
int curx = 0, cury = 0, theight = 0;
texturepixel fontdata[FONTTEXTURESIZE][FONTTEXTURESIZE];
2017-07-10 18:47:38 +00:00
void sdltogl(SDL_Surface *txt, glfont_t& f, int ch) {
2017-07-22 23:33:27 +00:00
#if CAP_TABFONT
2018-09-04 21:27:51 +00:00
if(ch < 32) return;
int otwidth, otheight, tpixindex = 0;
unsigned char tpix[3000];
2017-07-10 18:47:38 +00:00
loadCompressedChar(otwidth, otheight, tpix);
#else
2018-09-04 21:27:51 +00:00
if(!txt) return;
2017-07-10 18:47:38 +00:00
int otwidth = txt->w;
int otheight = txt->h;
#endif
2018-09-04 21:27:51 +00:00
if(otwidth+curx > FONTTEXTURESIZE) curx = 0, cury += theight, theight = 0;
theight = max(theight, otheight);
for(int j=0; j<otheight;j++) for(int i=0; i<otwidth; i++) {
fontdata[j+cury][i+curx] =
2017-07-22 23:33:27 +00:00
#if CAP_TABFONT
(i>=otwidth || j>=otheight) ? 0 : (tpix[tpixindex++] * 0x100) | 0xFF;
2017-07-10 18:47:38 +00:00
#else
2018-09-04 21:27:51 +00:00
((i>=txt->w || j>=txt->h) ? 0 : ((qpixel(txt, i, j)>>24)&0xFF) * 0x100) | 0x00FF;
2017-07-10 18:47:38 +00:00
#endif
}
f.widths[ch] = otwidth;
f.heights[ch] = otheight;
2018-09-04 21:27:51 +00:00
f.tx0[ch] = (float) curx / (float) FONTTEXTURESIZE;
f.tx1[ch] = (float) (curx+otwidth) / (float) FONTTEXTURESIZE;
f.ty0[ch] = (float) cury;
f.ty1[ch] = (float) (cury+otheight);
curx += otwidth;
2017-07-10 18:47:38 +00:00
}
void init_glfont(int size) {
if(glfont[size]) return;
2019-05-12 23:57:40 +00:00
DEBBI(DF_GRAPH, ("init GL font: ", size));
2018-09-04 21:27:51 +00:00
2017-07-22 23:33:27 +00:00
#if !CAP_TABFONT
loadfont(size);
if(!font[size]) return;
#endif
2017-07-10 18:47:38 +00:00
glfont[size] = new glfont_t;
glfont_t& f(*(glfont[size]));
//f.list_base = glGenLists(128);
2018-09-04 21:27:51 +00:00
glGenTextures(1, &f.texture );
2017-07-10 18:47:38 +00:00
2017-07-22 23:33:27 +00:00
#if !CAP_TABFONT
2017-07-10 18:47:38 +00:00
char str[2]; str[1] = 0;
SDL_Color white;
white.r = white.g = white.b = 255;
#endif
2018-09-04 21:50:50 +00:00
#if CAP_TABFONT
resetTabFont();
#endif
2017-07-10 18:47:38 +00:00
// glListBase(0);
2018-09-04 21:27:51 +00:00
curx = 0, cury = 0, theight = 0;
for(int ch=1;ch<CHARS;ch++) {
2017-07-10 18:47:38 +00:00
if(ch<32) continue;
2017-07-22 23:33:27 +00:00
#if CAP_TABFONT
2017-07-10 18:47:38 +00:00
sdltogl(NULL, f, ch);
#else
SDL_Surface *txt;
if(ch < 128) {
str[0] = ch;
txt = TTF_RenderText_Blended(font[size], str, white);
}
else {
txt = TTF_RenderUTF8_Blended(font[size], natchars[ch-128], white);
}
if(txt == NULL) continue;
2017-07-22 23:33:27 +00:00
#if CAP_CREATEFONT
2017-07-10 18:47:38 +00:00
generateFont(ch, txt);
#endif
sdltogl(txt, f, ch);
SDL_FreeSurface(txt);
#endif
}
2018-09-04 21:27:51 +00:00
glBindTexture( GL_TEXTURE_2D, f.texture);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
theight = next_p2(cury + theight);
glTexImage2D( GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, FONTTEXTURESIZE, theight, 0,
2018-09-04 21:27:51 +00:00
GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE,
fontdata);
for(int ch=0; ch<CHARS; ch++) f.ty0[ch] /= theight, f.ty1[ch] /= theight;
#if CAP_CREATEFONT
printf("#define NUMEXTRA %d\n", NUMEXTRA);
#define DEMACRO(x) #x
printf("#define NATCHARS " DEMACRO(NATCHARS) "\n");
#endif
2017-07-10 18:47:38 +00:00
//printf("init size=%d ok\n", size);
GLERR("initfont");
}
int gl_width(int size, const char *s) {
int gsiz = size;
if(size > vid.fsize || size > 72) gsiz = 72;
2017-07-22 23:33:27 +00:00
#if CAP_FIXEDSIZE
gsiz = CAP_FIXEDSIZE;
2017-07-10 18:47:38 +00:00
#endif
init_glfont(gsiz);
2017-07-22 23:33:27 +00:00
if(!glfont[gsiz]) return 0;
2017-07-10 18:47:38 +00:00
glfont_t& f(*glfont[gsiz]);
int x = 0;
for(int i=0; s[i];) {
int tabid = getnext(s,i);
x += f.widths[tabid] * size/gsiz;
}
return x;
}
2018-02-11 18:08:17 +00:00
namespace glhr { void texture_vertices(GLfloat *f, int qty, int stride = 2) {
#if CAP_SHADER
glVertexAttribPointer(aTexture, stride, GL_FLOAT, GL_FALSE, stride * sizeof(GLfloat), f);
#else
glTexCoordPointer(stride, GL_FLOAT, 0, f);
#endif
}
void oldvertices(GLfloat *f, int qty) {
#if CAP_SHADER
2019-02-22 19:58:40 +00:00
glVertexAttribPointer(aPosition, SHDIM, GL_FLOAT, GL_FALSE, SHDIM * sizeof(GLfloat), f);
2018-02-11 18:08:17 +00:00
#else
2019-02-22 19:58:40 +00:00
glVertexPointer(SHDIM, GL_FLOAT, 0, f);
2018-02-11 18:08:17 +00:00
#endif
// #endif
}
}
2018-09-04 21:27:51 +00:00
vector<glhr::textured_vertex> tver;
glhr::textured_vertex charvertex(int x1, int y1, ld tx, ld ty) {
glhr::textured_vertex res;
res.coords[0] = x1;
res.coords[1] = y1;
res.coords[2] = 0;
2019-02-22 19:58:40 +00:00
res.coords[3] = 1;
2018-09-04 21:27:51 +00:00
res.texture[0] = tx;
res.texture[1] = ty;
return res;
}
2017-07-10 18:47:38 +00:00
bool gl_print(int x, int y, int shift, int size, const char *s, color_t color, int align) {
2017-07-10 18:47:38 +00:00
int gsiz = size;
if(size > vid.fsize || size > 72) gsiz = 72;
2017-07-22 23:33:27 +00:00
#if CAP_FIXEDSIZE
gsiz = CAP_FIXEDSIZE;
2017-07-10 18:47:38 +00:00
#endif
init_glfont(gsiz);
2017-07-22 23:33:27 +00:00
if(!glfont[gsiz]) return false;
2017-07-10 18:47:38 +00:00
glfont_t& f(*glfont[gsiz]);
int tsize = 0;
for(int i=0; s[i];) {
tsize += f.widths[getnext(s,i)] * size/gsiz;
}
x -= tsize * align / 16;
y += f.heights[32] * size / (gsiz*2);
int ysiz = f.heights[32] * size / gsiz;
bool clicked = (mousex >= x && mousey <= y && mousex <= x+tsize && mousey >= y-ysiz);
2018-09-04 21:27:51 +00:00
color_t icolor = (color << 8) | 0xFF;
if(icolor != text_color || f.texture != text_texture || shift != text_shift || shapes_merged) {
glflush();
text_color = icolor;
text_texture = f.texture;
text_shift = shift;
}
texts_merged++;
auto& tver = text_vertices;
glBindTexture(GL_TEXTURE_2D, f.texture);
2017-07-10 18:47:38 +00:00
for(int i=0; s[i];) {
int tabid = getnext(s,i);
int wi = f.widths[tabid] * size/gsiz;
int hi = f.heights[tabid] * size/gsiz;
GLERR("pre-print");
2018-09-04 21:27:51 +00:00
glhr::textured_vertex t00 = charvertex(x, y-hi, f.tx0[tabid], f.ty0[tabid]);
glhr::textured_vertex t01 = charvertex(x, y, f.tx0[tabid], f.ty1[tabid]);
glhr::textured_vertex t11 = charvertex(x+wi, y, f.tx1[tabid], f.ty1[tabid]);
glhr::textured_vertex t10 = charvertex(x+wi, y-hi, f.tx1[tabid], f.ty0[tabid]);
2017-07-10 18:47:38 +00:00
2018-09-04 21:27:51 +00:00
tver.push_back(t00);
tver.push_back(t01);
tver.push_back(t10);
tver.push_back(t10);
tver.push_back(t01);
tver.push_back(t11);
x += wi;
2017-07-10 18:47:38 +00:00
}
2018-09-04 21:27:51 +00:00
2017-07-10 18:47:38 +00:00
return clicked;
}
2018-02-11 18:08:17 +00:00
2017-07-10 18:47:38 +00:00
#endif
purehookset hooks_resetGL;
void resetGL() {
2019-05-12 23:57:40 +00:00
DEBBI(DF_INIT | DF_GRAPH, ("reset GL"))
2017-07-10 18:47:38 +00:00
callhooks(hooks_resetGL);
2017-07-22 23:33:27 +00:00
#if CAP_GLFONT
2017-07-10 18:47:38 +00:00
for(int i=0; i<128; i++) if(glfont[i]) {
delete glfont[i];
glfont[i] = NULL;
}
#endif
#if MAXMDIM >= 4
if(floor_textures) {
delete floor_textures;
floor_textures = NULL;
}
2017-07-10 18:47:38 +00:00
#endif
resetGeometry(); // includes buildPoly
2017-07-10 18:47:38 +00:00
}
#endif
2017-07-22 23:33:27 +00:00
#if CAP_XGD
vector<int> graphdata;
void gdpush(int t) {
graphdata.push_back(t);
}
bool displaychr(int x, int y, int shift, int size, char chr, color_t col) {
2017-07-22 23:33:27 +00:00
gdpush(2); gdpush(x); gdpush(y); gdpush(8);
gdpush(col); gdpush(size); gdpush(0);
gdpush(1); gdpush(chr);
return false;
}
void gdpush_utf8(const string& s) {
int g = (int) graphdata.size(), q = 0;
2018-06-22 12:47:24 +00:00
gdpush((int) s.size()); for(int i=0; i<isize(s); i++) {
2017-07-22 23:33:27 +00:00
#if ISANDROID
unsigned char uch = (unsigned char) s[i];
if(uch >= 192 && uch < 224) {
int u = ((s[i] - 192)&31) << 6;
i++;
u += (s[i] - 128) & 63;
gdpush(u); q++;
}
else
#endif
{
gdpush(s[i]); q++;
}
}
graphdata[g] = q;
}
bool displayfr(int x, int y, int b, int size, const string &s, color_t color, int align) {
2017-07-22 23:33:27 +00:00
gdpush(2); gdpush(x); gdpush(y); gdpush(align);
gdpush(color); gdpush(size); gdpush(b);
gdpush_utf8(s);
int mx = mousex - x;
int my = mousey - y;
int len = textwidth(size, s);
return
mx >= -len*align/32 && mx <= +len*(16-align)/32 &&
my >= -size*3/4 && my <= +size*3/4;
}
bool displaystr(int x, int y, int shift, int size, const string &s, color_t color, int align) {
2017-07-22 23:33:27 +00:00
return displayfr(x,y,0,size,s,color,align);
}
bool displaystr(int x, int y, int shift, int size, char const *s, color_t color, int align) {
2017-07-22 23:33:27 +00:00
return displayfr(x,y,0,size,s,color,align);
}
#else
bool displaystr(int x, int y, int shift, int size, const char *str, color_t color, int align) {
2017-07-10 18:47:38 +00:00
if(strlen(str) == 0) return false;
if(size < 4 || size > 255) {
return false;
}
2017-07-22 23:33:27 +00:00
#if CAP_GLFONT
2017-07-10 18:47:38 +00:00
if(vid.usingGL) return gl_print(x, y, shift, size, str, color, align);
#endif
2017-07-22 23:33:27 +00:00
#if !CAP_SDLTTF
2017-07-10 18:47:38 +00:00
static bool towarn = true;
if(towarn) towarn = false, printf("WARNING: NOTTF works only with OpenGL!\n");
return false;
#else
SDL_Color col;
col.r = (color >> 16) & 255;
col.g = (color >> 8 ) & 255;
col.b = (color >> 0 ) & 255;
col.r >>= darken; col.g >>= darken; col.b >>= darken;
loadfont(size);
SDL_Surface *txt = ((vid.antialias & AA_FONT)?TTF_RenderUTF8_Blended:TTF_RenderUTF8_Solid)(font[size], str, col);
if(txt == NULL) return false;
SDL_Rect rect;
rect.w = txt->w;
rect.h = txt->h;
rect.x = x - rect.w * align / 16;
rect.y = y - rect.h/2;
bool clicked = (mousex >= rect.x && mousey >= rect.y && mousex <= rect.x+rect.w && mousey <= rect.y+rect.h);
if(shift) {
SDL_Surface* txt2 = SDL_DisplayFormat(txt);
SDL_LockSurface(txt2);
SDL_LockSurface(s);
color_t c0 = qpixel(txt2, 0, 0);
2017-07-10 18:47:38 +00:00
for(int yy=0; yy<rect.h; yy++)
for(int xx=0; xx<rect.w; xx++) if(qpixel(txt2, xx, yy) != c0)
qpixel(s, rect.x+xx-shift, rect.y+yy) |= color & 0xFF0000,
qpixel(s, rect.x+xx+shift, rect.y+yy) |= color & 0x00FFFF;
SDL_UnlockSurface(s);
SDL_UnlockSurface(txt2);
SDL_FreeSurface(txt2);
}
else {
SDL_BlitSurface(txt, NULL, s,&rect);
}
SDL_FreeSurface(txt);
return clicked;
#endif
}
bool displaystr(int x, int y, int shift, int size, const string &s, color_t color, int align) {
2017-07-10 18:47:38 +00:00
return displaystr(x, y, shift, size, s.c_str(), color, align);
}
bool displayfrSP(int x, int y, int sh, int b, int size, const string &s, color_t color, int align, int p) {
2017-07-10 18:47:38 +00:00
if(b) {
displaystr(x-b, y, 0, size, s, p, align);
displaystr(x+b, y, 0, size, s, p, align);
displaystr(x, y-b, 0, size, s, p, align);
displaystr(x, y+b, 0, size, s, p, align);
}
if(b >= 2) {
int b1 = b-1;
displaystr(x-b1, y-b1, 0, size, s, p, align);
displaystr(x-b1, y+b1, 0, size, s, p, align);
displaystr(x+b1, y-b1, 0, size, s, p, align);
displaystr(x+b1, y+b1, 0, size, s, p, align);
}
return displaystr(x, y, 0, size, s, color, align);
}
bool displayfr(int x, int y, int b, int size, const string &s, color_t color, int align) {
2017-07-10 18:47:38 +00:00
return displayfrSP(x, y, 0, b, size, s, color, align, poly_outline>>8);
}
bool displaychr(int x, int y, int shift, int size, char chr, color_t col) {
2017-07-10 18:47:38 +00:00
char buf[2];
buf[0] = chr; buf[1] = 0;
return displaystr(x, y, shift, size, buf, col, 8);
}
#endif
bool displaynum(int x, int y, int shift, int size, color_t col, int val, string title) {
2017-07-10 18:47:38 +00:00
char buf[64];
sprintf(buf, "%d", val);
bool b1 = displayfr(x-8, y, 1, size, buf, col, 16);
bool b2 = displayfr(x, y, 1, size, title, col, 0);
if((b1 || b2) && gtouched) {
col ^= 0x00FFFF;
displayfr(x-8, y, 1, size, buf, col, 16);
displayfr(x, y, 1, size, title, col, 0);
}
return b1 || b2;
}
vector<msginfo> msgs;
vector<msginfo> gamelog;
void flashMessages() {
2018-06-22 12:47:24 +00:00
for(int i=0; i<isize(msgs); i++)
2017-07-10 18:47:38 +00:00
if(msgs[i].stamp < ticks - 1000 && !msgs[i].flashout) {
msgs[i].flashout = true;
msgs[i].stamp = ticks;
}
}
string fullmsg(msginfo& m) {
string s = m.msg;
if(m.quantity > 1) s += " (x" + its(m.quantity) + ")";
return s;
}
2017-07-10 18:47:38 +00:00
void addMessageToLog(msginfo& m, vector<msginfo>& log) {
2018-06-22 12:47:24 +00:00
if(isize(log) != 0) {
msginfo& last = log[isize(log)-1];
2017-07-10 18:47:38 +00:00
if(last.msg == m.msg) {
int q = m.quantity + last.quantity;
last = m; last.quantity = q;
return;
}
}
2018-06-22 12:47:24 +00:00
if(isize(log) < 1000)
2017-07-10 18:47:38 +00:00
log.push_back(m);
else {
2018-06-22 12:47:24 +00:00
for(int i=0; i<isize(log)-1; i++) swap(log[i], log[i+1]);
log[isize(log)-1] = m;
2017-07-10 18:47:38 +00:00
}
}
void clearMessages() { msgs.clear(); }
void addMessage(string s, char spamtype) {
2019-05-12 23:57:40 +00:00
DEBB(DF_MSG, ("addMessage: ", s));
2017-07-10 18:47:38 +00:00
msginfo m;
m.msg = s; m.spamtype = spamtype; m.flashout = false; m.stamp = ticks;
m.rtstamp = time(NULL);
m.gtstamp = getgametime();
m.turnstamp = turncount;
2017-07-10 18:47:38 +00:00
m.quantity = 1;
addMessageToLog(m, gamelog);
addMessageToLog(m, msgs);
}
color_t colormix(color_t a, color_t b, color_t c) {
2017-07-10 18:47:38 +00:00
for(int p=0; p<3; p++)
part(a, p) = part(a,p) + (part(b,p) - part(a,p)) * part(c,p) / 255;
return a;
}
2017-10-08 12:49:49 +00:00
int rhypot(int a, int b) { return (int) sqrt(a*a - b*b); }
2017-11-07 12:40:56 +00:00
ld realradius() {
ld vradius = current_display->radius;
if(sphere) {
if(sphereflipped())
2018-02-03 12:41:49 +00:00
vradius /= sqrt(vid.alpha*vid.alpha - 1);
else
vradius = 1e12; // use the following
}
if(euclid)
vradius = current_display->radius * get_sightrange() / (1 + vid.alpha) / 2.5;
2017-11-07 12:40:56 +00:00
vradius = min<ld>(vradius, min(vid.xres, vid.yres) / 2);
return vradius;
}
void drawmessage(const string& s, int& y, color_t col) {
2017-11-07 12:40:56 +00:00
int rrad = (int) realradius();
2017-10-08 12:49:49 +00:00
int space;
if(y > current_display->ycenter + rrad * vid.stretch)
2017-10-08 12:49:49 +00:00
space = vid.xres;
else if(y > current_display->ycenter)
space = current_display->xcenter - rhypot(rrad, (y-current_display->ycenter) / vid.stretch);
else if(y > current_display->ycenter - vid.fsize)
space = current_display->xcenter - rrad;
else if(y > current_display->ycenter - vid.fsize - rrad * vid.stretch)
space = current_display->xcenter - rhypot(rrad, (current_display->ycenter-vid.fsize-y) / vid.stretch);
2017-10-08 12:49:49 +00:00
else
space = vid.xres;
if(textwidth(vid.fsize, s) <= space) {
displayfr(0, y, 1, vid.fsize, s, col, 0);
y -= vid.fsize;
return;
}
2018-06-22 12:47:24 +00:00
for(int i=1; i<isize(s); i++)
2017-10-08 12:49:49 +00:00
if(s[i-1] == ' ' && textwidth(vid.fsize, "..."+s.substr(i)) <= space) {
displayfr(0, y, 1, vid.fsize, "..."+s.substr(i), col, 0);
y -= vid.fsize;
drawmessage(s.substr(0, i-1), y, col);
return;
}
// no chance
displayfr(0, y, 1, vid.fsize, s, col, 0);
y -= vid.fsize;
return;
}
2017-07-10 18:47:38 +00:00
void drawmessages() {
2019-05-12 23:57:40 +00:00
DEBBI(DF_GRAPH, ("draw messages"));
2017-07-10 18:47:38 +00:00
int i = 0;
int t = ticks;
2018-06-22 12:47:24 +00:00
for(int j=0; j<isize(msgs); j++) {
if(j < isize(msgs) - vid.msglimit) continue;
2017-07-10 18:47:38 +00:00
int age = msgs[j].flashout * (t - msgs[j].stamp);
if(msgs[j].spamtype) {
2018-06-22 12:47:24 +00:00
for(int i=j+1; i<isize(msgs); i++) if(msgs[i].spamtype == msgs[j].spamtype)
2017-07-10 18:47:38 +00:00
msgs[j].flashout = 2;
}
2017-10-08 12:49:49 +00:00
if(age < 256*vid.flashtime)
msgs[i++] = msgs[j];
}
msgs.resize(i);
if(vid.msgleft == 2) {
int y = vid.yres - vid.fsize - (ISIOS ? 4 : 0);
2018-06-22 12:47:24 +00:00
for(int j=isize(msgs)-1; j>=0; j--) {
2017-10-08 12:49:49 +00:00
int age = msgs[j].flashout * (t - msgs[j].stamp);
poly_outline = gradient(bordcolor, backcolor, 0, age, 256*vid.flashtime) << 8;
color_t col = gradient(forecolor, backcolor, 0, age, 256*vid.flashtime);
drawmessage(fullmsg(msgs[j]), y, col);
2017-10-08 12:49:49 +00:00
}
}
else {
2018-06-22 12:47:24 +00:00
for(int j=0; j<isize(msgs); j++) {
2017-10-08 12:49:49 +00:00
int age = msgs[j].flashout * (t - msgs[j].stamp);
int x = vid.msgleft ? 0 : vid.xres / 2;
2018-06-22 12:47:24 +00:00
int y = vid.yres - vid.fsize * (isize(msgs) - j) - (ISIOS ? 4 : 0);
2017-07-10 18:47:38 +00:00
poly_outline = gradient(bordcolor, backcolor, 0, age, 256*vid.flashtime) << 8;
displayfr(x, y, 1, vid.fsize, fullmsg(msgs[j]), gradient(forecolor, backcolor, 0, age, 256*vid.flashtime), vid.msgleft ? 0 : 8);
2017-07-10 18:47:38 +00:00
}
}
}
color_t gradient(color_t c0, color_t c1, ld v0, ld v, ld v1) {
2017-07-10 18:47:38 +00:00
int vv = int(256 * ((v-v0) / (v1-v0)));
color_t c = 0;
2017-07-10 18:47:38 +00:00
for(int a=0; a<3; a++) {
int p0 = part(c0, a);
int p1 = part(c1, a);
part(c, a) = (p0*(256-vv) + p1*vv + 127) >> 8;
2017-07-10 18:47:38 +00:00
}
return c;
}
2018-11-08 15:28:17 +00:00
void drawCircle(int x, int y, int size, color_t color, color_t fillcolor) {
2017-07-10 18:47:38 +00:00
if(size < 0) size = -size;
2019-02-17 17:41:40 +00:00
#if CAP_GL && CAP_POLY
2017-07-10 18:47:38 +00:00
if(vid.usingGL) {
2018-09-04 21:27:27 +00:00
glflush();
2018-02-08 23:29:20 +00:00
glhr::be_nontextured();
glhr::id_modelview();
2019-05-09 14:58:42 +00:00
dynamicval<eModel> em(pmodel, mdUnchanged);
2018-02-11 18:08:17 +00:00
glcoords.clear();
x -= current_display->xcenter; y -= current_display->ycenter;
2017-07-10 18:47:38 +00:00
int pts = size * 4;
if(pts > 1500) pts = 1500;
if(ISMOBILE && pts > 72) pts = 72;
for(int r=0; r<pts; r++) {
float rr = (M_PI * 2 * r) / pts;
2019-02-22 19:58:40 +00:00
glcoords.push_back(glhr::makevertex(x + size * sin(rr), y + size * vid.stretch * cos(rr), current_display->scrdist));
2017-07-10 18:47:38 +00:00
}
2019-05-09 14:58:42 +00:00
current_display->set_all(0);
2018-02-11 18:08:17 +00:00
glhr::vertices(glcoords);
glhr::set_depthtest(false);
2018-11-08 15:28:17 +00:00
if(fillcolor) {
glhr::color2(fillcolor);
glDrawArrays(GL_TRIANGLE_FAN, 0, pts);
}
if(color) {
glhr::color2(color);
glDrawArrays(GL_LINE_LOOP, 0, pts);
}
2017-07-10 18:47:38 +00:00
return;
}
#endif
2017-07-22 23:33:27 +00:00
#if CAP_XGD
2017-07-10 18:47:38 +00:00
gdpush(4); gdpush(color); gdpush(x); gdpush(y); gdpush(size);
2017-07-22 23:33:27 +00:00
#elif CAP_SDLGFX
2018-11-08 15:28:17 +00:00
if(vid.stretch == 1) {
if(fillcolor) filledCircleColor(s, x, y, size, fillcolor);
if(color) ((vid.antialias && AA_NOGL)?aacircleColor:circleColor) (s, x, y, size, color);
}
else {
if(fillcolor) filledEllipseColor(s, x, y, size, size * vid.stretch, fillcolor);
if(color) ((vid.antialias && AA_NOGL)?aaellipseColor:ellipseColor) (s, x, y, size, size * vid.stretch, color);
}
2017-07-22 23:33:27 +00:00
#elif CAP_SDL
2017-07-10 18:47:38 +00:00
int pts = size * 4;
if(pts > 1500) pts = 1500;
for(int r=0; r<pts; r++)
qpixel(s, x + int(size * sin(r)), y + int(size * cos(r))) = color;
#endif
}
void displayButton(int x, int y, const string& name, int key, int align, int rad) {
if(displayfr(x, y, rad, vid.fsize, name, 0x808080, align)) {
displayfr(x, y, rad, vid.fsize, name, 0xFFFF00, align);
getcstat = key;
}
}
char mousekey = 'n';
char newmousekey;
void displaymm(char c, int x, int y, int rad, int size, const string& title, int align) {
if(displayfr(x, y, rad, size, title, c == mousekey ? 0xFF8000 : 0xC0C0C0, align)) {
displayfr(x, y, rad, size, title, 0xFFFF00, align);
getcstat = SETMOUSEKEY, newmousekey = c;
}
}
bool displayButtonS(int x, int y, const string& name, color_t col, int align, int size) {
2017-07-10 18:47:38 +00:00
if(displaystr(x, y, 0, size, name, col, align)) {
displaystr(x, y, 0, size, name, 0xFFFF00, align);
return true;
}
else return false;
}
void displayColorButton(int x, int y, const string& name, int key, int align, int rad, color_t color, color_t color2) {
2017-07-10 18:47:38 +00:00
if(displayfr(x, y, rad, vid.fsize, name, color, align)) {
if(color2) displayfr(x, y, rad, vid.fsize, name, color2, align);
getcstat = key;
}
}
ld textscale() {
return vid.fsize / (current_display->radius * crossf) * (1+vid.alpha) * 2;
2017-07-10 18:47:38 +00:00
}
// bool notgl = false;
bool setfsize = true;
2018-02-20 10:41:55 +00:00
bool vsync_off;
void do_setfsize() {
vid.fsize = min(vid.yres * fontscale/ 3200, vid.xres * fontscale/ 4800), setfsize = false;
}
2018-02-20 10:41:55 +00:00
void disable_vsync() {
2018-02-20 20:10:26 +00:00
#if !ISMOBWEB
2018-02-20 10:41:55 +00:00
SDL_GL_SetAttribute( SDL_GL_SWAP_CONTROL, 0 );
2018-02-20 20:10:26 +00:00
#endif
2018-02-20 10:41:55 +00:00
}
2017-12-01 23:19:34 +00:00
#if CAP_SDL
2017-07-10 18:47:38 +00:00
void setvideomode() {
2019-05-12 23:57:40 +00:00
DEBBI(DF_INIT | DF_GRAPH, ("setvideomode"));
2017-07-10 18:47:38 +00:00
if(!vid.full) {
if(vid.xres > vid.xscr) vid.xres = vid.xscr * 9/10, setfsize = true;
if(vid.yres > vid.yscr) vid.yres = vid.yscr * 9/10, setfsize = true;
}
if(setfsize) do_setfsize();
2017-07-10 18:47:38 +00:00
int flags = 0;
2017-07-22 23:33:27 +00:00
#if CAP_GL
2017-07-10 18:47:38 +00:00
if(vid.usingGL) {
flags = SDL_OPENGL | SDL_HWSURFACE | SDL_GL_DOUBLEBUFFER;
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 1);
2018-02-20 10:41:55 +00:00
if(vsync_off) disable_vsync();
2017-07-12 16:03:53 +00:00
if(vid.antialias & AA_MULTI) {
2017-07-10 18:47:38 +00:00
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
2017-08-06 12:50:16 +00:00
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, (vid.antialias & AA_MULTI16) ? 16 : 4);
2017-07-10 18:47:38 +00:00
}
}
#endif
int sizeflag = (vid.full ? SDL_FULLSCREEN : SDL_RESIZABLE);
s = s_screen = SDL_SetVideoMode(vid.xres, vid.yres, 32, flags | sizeflag);
2017-07-10 18:47:38 +00:00
if(vid.full && !s) {
vid.xres = vid.xscr;
vid.yres = vid.yscr;
do_setfsize();
s = s_screen = SDL_SetVideoMode(vid.xres, vid.yres, 32, flags | SDL_FULLSCREEN);
2017-07-10 18:47:38 +00:00
}
if(!s) {
addMessage("Failed to set the graphical mode: "+its(vid.xres)+"x"+its(vid.yres)+(vid.full ? " fullscreen" : " windowed"));
vid.xres = 640;
vid.yres = 480;
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);
vid.antialias &= ~AA_MULTI;
s = s_screen = SDL_SetVideoMode(vid.xres, vid.yres, 32, flags | SDL_RESIZABLE);
2017-07-10 18:47:38 +00:00
}
2017-07-22 23:33:27 +00:00
#if CAP_GL
2017-07-10 18:47:38 +00:00
if(vid.usingGL) {
2019-05-06 23:10:11 +00:00
if(vid.antialias & AA_MULTI) {
2017-07-22 23:33:27 +00:00
glEnable(GL_MULTISAMPLE);
2019-05-06 23:10:11 +00:00
glEnable(GL_MULTISAMPLE_ARB);
}
else {
2017-07-22 23:33:27 +00:00
glDisable(GL_MULTISAMPLE);
2019-05-06 23:10:11 +00:00
glDisable(GL_MULTISAMPLE_ARB);
}
2017-07-22 23:33:27 +00:00
2017-07-10 18:47:38 +00:00
glViewport(0, 0, vid.xres, vid.yres);
2018-02-08 23:29:20 +00:00
glhr::init();
2017-07-10 18:47:38 +00:00
resetGL();
}
#endif
}
#endif
bool noGUI = false;
void initgraph() {
2019-05-12 23:57:40 +00:00
DEBBI(DF_INIT | DF_GRAPH, ("initgraph"));
2017-07-10 18:47:38 +00:00
initConfig();
2017-07-22 23:33:27 +00:00
#if CAP_SDLJOY
2017-07-10 18:47:38 +00:00
joyx = joyy = 0; joydir.d = -1;
2017-07-22 23:33:27 +00:00
#endif
2017-07-10 18:47:38 +00:00
restartGraph();
if(noGUI) {
2017-07-22 23:33:27 +00:00
#if CAP_COMMANDLINE
2017-07-10 18:47:38 +00:00
arg::read(2);
#endif
return;
}
2017-07-22 23:33:27 +00:00
#if CAP_SDL
2017-07-10 18:47:38 +00:00
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) == -1)
{
printf("Failed to initialize video.\n");
exit(2);
}
2017-07-22 23:33:27 +00:00
#if ISWEB
2017-07-10 18:47:38 +00:00
vid.xscr = vid.xres = 1200;
vid.yscr = vid.yres = 900;
#else
const SDL_VideoInfo *inf = SDL_GetVideoInfo();
vid.xscr = vid.xres = inf->current_w;
vid.yscr = vid.yres = inf->current_h;
#endif
#ifdef CUSTOM_CAPTION
SDL_WM_SetCaption(CUSTOM_CAPTION, CUSTOM_CAPTION);
#else
2017-07-10 18:47:38 +00:00
SDL_WM_SetCaption("HyperRogue " VER, "HyperRogue " VER);
#endif
2017-07-22 23:33:27 +00:00
#endif
2017-07-10 18:47:38 +00:00
preparesort();
2017-07-22 23:33:27 +00:00
#if CAP_CONFIG
2017-07-10 18:47:38 +00:00
loadConfig();
#endif
2017-07-22 23:33:27 +00:00
#if CAP_COMMANDLINE
2017-07-10 18:47:38 +00:00
arg::read(2);
#endif
2018-08-18 15:53:06 +00:00
precalc();
2017-07-10 18:47:38 +00:00
2017-07-22 23:33:27 +00:00
#if CAP_SDL
2017-07-10 18:47:38 +00:00
setvideomode();
if(!s) {
printf("Failed to initialize graphics.\n");
exit(2);
}
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
SDL_EnableUNICODE(1);
2017-07-22 23:33:27 +00:00
#endif
2017-07-10 18:47:38 +00:00
2017-07-22 23:33:27 +00:00
#if CAP_SDLTTF
2017-07-10 18:47:38 +00:00
if(TTF_Init() != 0) {
printf("Failed to initialize TTF.\n");
exit(2);
}
#endif
2017-07-22 23:33:27 +00:00
#if CAP_SDLJOY
2017-07-10 18:47:38 +00:00
initJoysticks();
2017-07-22 23:33:27 +00:00
#endif
2017-07-10 18:47:38 +00:00
2017-07-22 23:33:27 +00:00
#if CAP_SDLAUDIO
2017-07-10 18:47:38 +00:00
initAudio();
2017-07-22 23:33:27 +00:00
#endif
2017-07-10 18:47:38 +00:00
}
void cleargraph() {
2019-05-12 23:57:40 +00:00
DEBBI(DF_INIT, ("clear graph"));
2017-07-22 23:33:27 +00:00
#if CAP_SDLTTF
2017-07-10 18:47:38 +00:00
for(int i=0; i<256; i++) if(font[i]) TTF_CloseFont(font[i]);
#endif
2017-07-22 23:33:27 +00:00
#if CAL_GLFONT
2017-07-10 18:47:38 +00:00
for(int i=0; i<128; i++) if(glfont[i]) delete glfont[i];
#endif
2017-07-22 23:33:27 +00:00
#if CAP_SDLJOY
2017-07-10 18:47:38 +00:00
closeJoysticks();
#endif
2017-07-22 23:33:27 +00:00
#if CAP_SDL
2017-07-10 18:47:38 +00:00
SDL_Quit();
#endif
}
int calcfps() {
#define CFPS 30
static int last[CFPS], lidx = 0;
int ct = ticks;
int ret = ct - last[lidx];
last[lidx] = ct;
lidx++; lidx %= CFPS;
if(ret == 0) return 0;
return (1000 * CFPS) / ret;
}
2019-03-09 15:20:06 +00:00
namespace subscreens {
vector<display_data> player_displays;
bool in;
int current_player;
bool is_current_player(int id) {
if(!in) return true;
return id == current_player;
}
void prepare() {
int N = multi::players;
if(N > 1) {
player_displays.resize(N, *current_display);
int qrows[10] = {1, 1, 1, 1, 2, 2, 2, 3, 3, 3};
int rows = qrows[N];
int cols = (N + rows - 1) / rows;
for(int i=0; i<N; i++) {
auto& pd = player_displays[i];
pd.xmin = (i % cols) * 1. / cols;
pd.xmax = ((i % cols) + 1.) / cols;
pd.ymin = (i / cols) * 1. / rows;
pd.ymax = ((i / cols) + 1.) / rows;
}
}
else {
player_displays.clear();
}
}
bool split(reaction_t what) {
using namespace racing;
if(in) return false;
if(!racing::on && !(shmup::on && DIM == 3)) return false;
if(!player_displays.empty()) {
in = true;
int& p = current_player;
for(p = 0; p < multi::players; p++) {
dynamicval<display_data*> c(current_display, &player_displays[p]);
what();
}
in = false;
return true;
}
return false;
}
}
}