From e8c670481c1e3c696bd64c44785e48269be8e5ba Mon Sep 17 00:00:00 2001 From: Julius Ikkala Date: Sat, 4 Mar 2023 22:06:24 +0200 Subject: [PATCH] Fix C variable declaration order stuff --- README.md | 3 +- curses.h | 4 +-- docs/README.md | 2 ++ gl/pdcdisp.c | 84 ++++++++++++++++++++++++++++++-------------------- gl/pdckbd.c | 9 +++--- gl/pdcscrn.c | 34 +++++++++++--------- 6 files changed, 81 insertions(+), 55 deletions(-) diff --git a/README.md b/README.md index 4ac8958e..847d7996 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ Welcome to PDCursesMod! ======================= -Public Domain Curses (Modified), aka PDCursesMod, is an implementation of X/Open curses for multiple platforms. As the name suggests, it is a modified version of the ['official' PDCurses project](https://www.github.com/wmcbrine/PDCurses), adding a variety of improvements, such as Windows GUI, VT-like console, Linux/*BSD framebuffer, DOSVGA, and Plan 9 "flavors" of Curses. It expands many of the capabilities of the flavors inherited from PDCurses (SDL1, SDL2, X11, Win32 console, OS/2, and DOS). Some details and screen shots are at +Public Domain Curses (Modified), aka PDCursesMod, is an implementation of X/Open curses for multiple platforms. As the name suggests, it is a modified version of the ['official' PDCurses project](https://www.github.com/wmcbrine/PDCurses), adding a variety of improvements, such as Windows GUI, VT-like console, Linux/*BSD framebuffer, DOSVGA, OpenGL, and Plan 9 "flavors" of Curses. It expands many of the capabilities of the flavors inherited from PDCurses (SDL1, SDL2, X11, Win32 console, OS/2, and DOS). Some details and screen shots are at https://www.projectpluto.com/win32a.htm @@ -34,6 +34,7 @@ Build instructions are in the README file for each platform: - [WinGUI](wingui/README.md) for use on Windows Graphics Mode - [X11](x11/README.md) (also called XCurses) for use as separate X11 window - [VT](vt/README.md) for use on terminal +- [OpenGL](gl/README.md) for use as separate window with OpenGL rendering Distribution Status ------------------- diff --git a/curses.h b/curses.h index 2f5ed3a0..95422349 100644 --- a/curses.h +++ b/curses.h @@ -524,8 +524,8 @@ a fourth bit is reserved. Default chtypes have enough character bits to support the full range of Unicode, all attributes, and 2^20 = 1048576 color pairs. Note, though, -that as of 2022 Jun 17, only WinGUI, VT, X11, Linux framebuffer, and -SDLn have COLOR_PAIRS = 1048576. Other platforms (DOSVGA, Plan9, WinCon) +that as of 2022 Jun 17, only WinGUI, VT, X11, Linux framebuffer, OpenGL, +and SDLn have COLOR_PAIRS = 1048576. Other platforms (DOSVGA, Plan9, WinCon) may join them. Some (DOS, OS/2) simply do not have full-color capability. diff --git a/docs/README.md b/docs/README.md index 815f81e0..c8c9c959 100644 --- a/docs/README.md +++ b/docs/README.md @@ -20,6 +20,7 @@ Also consult the README for each specific platform you'll be using: - [Windows in console mode (WinCon)] - [Windows in graphical mode (WinGUI)] - [X11] +- [OpenGL] Building -------- @@ -47,3 +48,4 @@ The files in this directory are released to the public domain. [Windows console]: ../wincon/README.md [Windows graphical]: ../wingui/README.md [X11]: ../x11/README.md +[OpenGL]: ../gl/README.md diff --git a/gl/pdcdisp.c b/gl/pdcdisp.c index fa097fd2..abb0d4e9 100644 --- a/gl/pdcdisp.c +++ b/gl/pdcdisp.c @@ -170,6 +170,7 @@ static void enlarge_glyph_cache() else { bool* visited = calloc(grid_w * grid_h * grid_layers, sizeof(bool)); + int attr; /* If we're here, it's not possible to enlarge the texture, so we have * to evict everything that's not needed out of the texture. This can @@ -178,13 +179,14 @@ static void enlarge_glyph_cache() for(i = 0; i < pdc_glyph_row_capacity; ++i) pdc_glyph_start_col[i] = 0; - for(int attr = 0; attr < 4; ++attr) + for(attr = 0; attr < 4; ++attr) for(i = 0; i < pdc_glyph_cache_size[attr]; ++i) { Uint32* cached_glyph = pdc_glyph_cache[attr]+i; Uint32 old_glyph = *cached_glyph; bool used = FALSE; int w = old_glyph >> 30; + int row; if(old_glyph == 0) continue; @@ -206,7 +208,7 @@ static void enlarge_glyph_cache() continue; /* Used glyphs are given a new index and copied over. */ - for(int row = 0; row < pdc_glyph_row_capacity; ++row) + for(row = 0; row < pdc_glyph_row_capacity; ++row) { int *col = &pdc_glyph_start_col[row]; if(*col + w <= pdc_glyph_col_capacity) @@ -258,8 +260,9 @@ static Uint32 alloc_glyph_cache(int w) /* Keep trying until we succeed. */ for(;;) { + int row; /* We try to fill the glyph cache in rows, from bottom to top. */ - for(int row = 0; row < pdc_glyph_row_capacity; ++row) + for(row = 0; row < pdc_glyph_row_capacity; ++row) { int *col = &pdc_glyph_start_col[row]; if(*col+w <= pdc_glyph_col_capacity) @@ -392,13 +395,13 @@ static Uint32 get_pdc_color( const int color_idx) static Uint32 get_glyph_texture_index(Uint32 ch32) { - /* Fullwidth dummy char! 0 makes it stop existing! */ - if(ch32 == 0x110000) return 0; - SDL_Color white = {255,255,255,255}; int *cache_size = &pdc_glyph_cache_size[cache_attr_index]; Uint32 **cache = &pdc_glyph_cache[cache_attr_index]; + /* Fullwidth dummy char! 0 makes it stop existing! */ + if(ch32 == 0x110000) return 0; + #ifndef PDC_SDL_SUPPLEMENTARY_PLANES_SUPPORT /* no support for supplementary planes */ if (ch32 > 0xffff) @@ -413,6 +416,8 @@ static Uint32 get_glyph_texture_index(Uint32 ch32) else { /* Here we need to render the character, it's not cached. */ + int w = 0; + Uint32 index; SDL_Surface* surf = NULL; #ifdef PDC_SDL_SUPPLEMENTARY_PLANES_SUPPORT @@ -424,8 +429,8 @@ static Uint32 get_glyph_texture_index(Uint32 ch32) /* Kind-of-fullwidthness-but-not-really: Italics can also overstep * and cause w = 2, which should still render completely fine. */ - int w = (surf->w + pdc_fwidth-1)/pdc_fwidth; - Uint32 index = alloc_glyph_cache(w); + w = (surf->w + pdc_fwidth-1)/pdc_fwidth; + index = alloc_glyph_cache(w); /* The SDL_Surface pitch may not match with the width, so we use this * to get glTexSubImage2D to deal with the proper pitch. */ @@ -469,7 +474,7 @@ static Uint32 get_glyph_texture_index(Uint32 ch32) } #ifdef USING_COMBINING_CHARACTER_SCHEME -int PDC_expand_combined_characters( const cchar_t c, cchar_t *added); /* addch.c */ +int PDC_expand_combined_characters( const cchar_t c, cchar_t *added); #endif static void draw_glyph( @@ -478,20 +483,22 @@ static void draw_glyph( Uint32 foreground ){ struct color_data* cd; +#ifdef USING_COMBINING_CHARACTER_SCHEME int layer = 0; +#endif int i = x + y * SP->cols; - if(y < 0 || y >= SP->lines || x < 0 || x >= SP->cols) - return; - - ensure_glyph_grid(1); - cd = &color_grid[i]; - cd->bg_color = background; Uint32 gl_attrs = ((attr & A_UNDERLINE) ? 1<<2 : 0) | ((attr & A_OVERLINE) ? 1<<3 : 0) | ((attr & A_STRIKEOUT) ? 1<<4 : 0) | ((attr & A_LEFT) ? 1<<5 : 0) | ((attr & A_RIGHT) ? 1<<6 : 0); + if(y < 0 || y >= SP->lines || x < 0 || x >= SP->cols) + return; + + ensure_glyph_grid(1); + cd = &color_grid[i]; + cd->bg_color = background; cd->fg_color = foreground | (gl_attrs << 24); #ifdef USING_COMBINING_CHARACTER_SCHEME @@ -529,12 +536,12 @@ static void draw_glyph( static void draw_cursor(int y, int x, int visibility) { struct color_data* cd; + Uint32 gl_attrs = visibility >= 0 && visibility <= 2 ? visibility : 0; if(y < 0 || y >= SP->lines || x < 0 || x >= SP->cols) return; ensure_glyph_grid(1); cd = &color_grid[x + y * SP->cols]; - Uint32 gl_attrs = visibility >= 0 && visibility <= 2 ? visibility : 0; cd->fg_color |= gl_attrs << 24; } @@ -717,6 +724,14 @@ void PDC_blink_text(void) void PDC_doupdate(void) { + int w, h; + SDL_Rect viewport = PDC_get_viewport(); + bool use_render_target = pdc_interpolation_mode == PDC_GL_INTERPOLATE_BILINEAR && + pdc_resize_mode != PDC_GL_RESIZE_NORMAL; + int u_screen_size, u_glyph_size, u_fthick, u_line_color; + short hcol = SP->line_color; + int layer; + ensure_glyph_grid(1); /* Upload grid buffers at the start, before we queue the commands that need @@ -737,18 +752,12 @@ void PDC_doupdate(void) GL_STREAM_DRAW ); - int w, h; SDL_GetWindowSize(pdc_window, &w, &h); glViewport(0, 0, w, h); glClearColor(0.0f,0.0f,0.0f,0.0f); glClear(GL_COLOR_BUFFER_BIT); - SDL_Rect viewport = PDC_get_viewport(); - bool use_render_target = - pdc_interpolation_mode == PDC_GL_INTERPOLATE_BILINEAR && - pdc_resize_mode != PDC_GL_RESIZE_NORMAL; - if(use_render_target) { /* For bilinear interpolation, we unfortunately need a temporary render @@ -756,6 +765,9 @@ void PDC_doupdate(void) * their edges would still appear sharp, as they could not blend * between cell edges. */ + int content_w = SP->cols * pdc_fwidth; + int content_h = SP->lines * pdc_fheight; + if(!pdc_render_target_texture) { /* Need to allocate the render target texture. */ @@ -763,9 +775,6 @@ void PDC_doupdate(void) cur_render_target_w = cur_render_target_h = 0; } - int content_w = SP->cols * pdc_fwidth; - int content_h = SP->lines * pdc_fheight; - if(cur_render_target_w != content_w || cur_render_target_h != content_h) { /* Need to resize the render target texture. */ @@ -815,10 +824,12 @@ void PDC_doupdate(void) /* Draw background colors */ glUseProgram(pdc_background_shader_program); - int u_screen_size = glGetUniformLocation(pdc_background_shader_program, "screen_size"); + u_screen_size = glGetUniformLocation( + pdc_background_shader_program, "screen_size"); glUniform2i(u_screen_size, SP->cols, SP->lines); - int u_glyph_size = glGetUniformLocation(pdc_background_shader_program, "glyph_size"); + u_glyph_size = glGetUniformLocation( + pdc_background_shader_program, "glyph_size"); glUniform2i(u_glyph_size, pdc_fwidth, pdc_fheight); glDrawArraysInstanced(GL_TRIANGLES, 0, 6, SP->lines * SP->cols); @@ -826,17 +837,20 @@ void PDC_doupdate(void) /* Prepare for drawing foreground glyphs. */ glUseProgram(pdc_foreground_shader_program); - u_screen_size = glGetUniformLocation(pdc_foreground_shader_program, "screen_size"); + u_screen_size = glGetUniformLocation( + pdc_foreground_shader_program, "screen_size"); glUniform2i(u_screen_size, SP->cols, SP->lines); - u_glyph_size = glGetUniformLocation(pdc_foreground_shader_program, "glyph_size"); + u_glyph_size = glGetUniformLocation( + pdc_foreground_shader_program, "glyph_size"); glUniform2i(u_glyph_size, pdc_fwidth, pdc_fheight); - int u_fthick = glGetUniformLocation(pdc_foreground_shader_program, "fthick"); + u_fthick = glGetUniformLocation(pdc_foreground_shader_program, "fthick"); glUniform1i(u_fthick, pdc_fthick); - int u_line_color = glGetUniformLocation(pdc_foreground_shader_program, "line_color"); - short hcol = SP->line_color; + u_line_color = glGetUniformLocation( + pdc_foreground_shader_program, "line_color"); + hcol = SP->line_color; if(hcol >= 0) { PACKED_RGB rgb = PDC_get_palette_entry(hcol); @@ -849,7 +863,7 @@ void PDC_doupdate(void) else glUniform3f(u_line_color, -1, -1, -1); /* Draw foreground colors, layer by layer. */ - for(int layer = 0; layer < grid_layers; ++layer) + for(layer = 0; layer < grid_layers; ++layer) { if(layer != 0) { @@ -898,7 +912,9 @@ void PDC_pump_and_peep(void) { SDL_Event event; - if(SDL_PeepEvents(&event, 1, SDL_PEEKEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) > 0) + int res = SDL_PeepEvents( + &event, 1, SDL_PEEKEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT); + if(res > 0) { if (SDL_WINDOWEVENT == event.type && (SDL_WINDOWEVENT_RESTORED == event.window.event || diff --git a/gl/pdckbd.c b/gl/pdckbd.c index 60f9be3b..3f972d05 100644 --- a/gl/pdckbd.c +++ b/gl/pdckbd.c @@ -335,6 +335,7 @@ static int _process_mouse_event(void) short shift_flags = 0; int wheel_x = 0, wheel_y = 0; int mouse_x = 0, mouse_y = 0; + SDL_Rect viewport = PDC_get_viewport(); memset(&SP->mouse_status, 0, sizeof(MOUSE_STATUS)); @@ -356,8 +357,6 @@ static int _process_mouse_event(void) SDL_GetMouseState( &event.motion.x, &event.motion.y); } - SDL_Rect viewport = PDC_get_viewport(); - if(viewport.w == 0) viewport.w = 1; if(viewport.h == 0) viewport.h = 1; mouse_x = (event.motion.x-viewport.x) * SP->cols / viewport.w; @@ -461,8 +460,10 @@ int PDC_get_key(void) } return PDC_get_function_key( FUNCTION_KEY_SHUT_DOWN); case SDL_WINDOWEVENT: - if (SDL_WINDOWEVENT_SIZE_CHANGED == event.window.event || SDL_WINDOWEVENT_RESIZED == event.window.event) - { + if( + SDL_WINDOWEVENT_SIZE_CHANGED == event.window.event || + SDL_WINDOWEVENT_RESIZED == event.window.event + ){ if(pdc_resize_mode == PDC_GL_RESIZE_NORMAL) { pdc_sheight = event.window.data2; diff --git a/gl/pdcscrn.c b/gl/pdcscrn.c index aceee9fc..848f71d1 100644 --- a/gl/pdcscrn.c +++ b/gl/pdcscrn.c @@ -144,13 +144,14 @@ static const char* pdc_foreground_fragment_shader_src = static void _clean(void) { + int i; if (pdc_ttffont) { TTF_CloseFont(pdc_ttffont); TTF_Quit(); pdc_ttffont = NULL; } - for(int i = 0; i < 4; ++i) + for(i = 0; i < 4; ++i) { if(pdc_glyph_cache[i]) free(pdc_glyph_cache[i]); @@ -213,20 +214,24 @@ static void _clean(void) pdc_sheight = pdc_swidth = 0; } -static void add_shader(GLuint shader_program, GLenum shader_type, const char* src) -{ +static void add_shader( + GLuint shader_program, + GLenum shader_type, + const char* src +){ + GLint status = GL_FALSE; GLuint shader = glCreateShader(shader_type); glShaderSource(shader, 1, &src, NULL); glCompileShader(shader); - GLint status = GL_FALSE; glGetShaderiv(shader, GL_COMPILE_STATUS, &status); if(status != GL_TRUE) { + char* err; GLsizei length = 0; glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length); - char* err = malloc(length+1); + err = malloc(length+1); glGetShaderInfoLog(shader, length+1, &length, err); fprintf(stderr, "%s shader compilation error: %s\n", (shader_type == GL_VERTEX_SHADER ? "Vertex" : "Fragment"), @@ -241,16 +246,17 @@ static void add_shader(GLuint shader_program, GLenum shader_type, const char* sr static void build_shader_program(GLuint shader_program) { + GLint status = GL_FALSE; glLinkProgram(shader_program); - GLint status = GL_FALSE; glGetProgramiv(shader_program, GL_LINK_STATUS, &status); if(status != GL_TRUE) { + char* err; GLsizei length = 0; glGetProgramiv(shader_program, GL_INFO_LOG_LENGTH, &length); - char* err = malloc(length+1); + err = malloc(length+1); glGetProgramInfoLog(shader_program, length+1, &length, err); fprintf(stderr, "Shader program linking: %s\n", err); exit(1); @@ -299,6 +305,7 @@ int _get_displaynum(void) int PDC_scr_open(void) { int displaynum = 0; + int h, w; const char *ptsz, *fname; PDC_LOG(("PDC_scr_open() - called\n")); @@ -412,7 +419,6 @@ int PDC_scr_open(void) SDL_SetWindowIcon(pdc_window, pdc_icon); - int h, w; SDL_GetWindowSize(pdc_window, &w, &h); if (!pdc_sheight) pdc_sheight = h; @@ -428,7 +434,7 @@ int PDC_scr_open(void) SDL_GL_SetSwapInterval(0); - // Load the GL functions we use. + /* Load the GL functions we use. */ load_gl_funcs(); /* Build foreground shader. */ @@ -489,7 +495,8 @@ int PDC_scr_open(void) glVertexAttribIPointer(2, 1, GL_INT, sizeof(Uint32), NULL); glVertexAttribDivisor(2, 1); - glUniform1i(glGetUniformLocation(pdc_foreground_shader_program, "glyphs"), 0); + glUniform1i( + glGetUniformLocation(pdc_foreground_shader_program, "glyphs"), 0); /* This FBO is not only used in the bilinear filtering mode, but also * temporarily in various glyph cache operations. @@ -625,15 +632,14 @@ void PDC_set_resize_limits( const int new_min_lines, const int new_max_lines, SDL_Rect PDC_get_viewport(void) { - int w, h; - SDL_GetWindowSize(pdc_window, &w, &h); - int content_w = SP->cols * pdc_fwidth; int content_h = SP->lines * pdc_fheight; int scale = 0; - + int w, h; SDL_Rect rect; + SDL_GetWindowSize(pdc_window, &w, &h); + switch(pdc_resize_mode) { case PDC_GL_RESIZE_NORMAL: