From 23ff2420fe9bb02152cdb47c90acf4262b863414 Mon Sep 17 00:00:00 2001 From: DeaDDooMER Date: Mon, 30 Mar 2020 22:03:45 +0400 Subject: [PATCH] render: remove dependency on sdl 1.2 --- src/gl/render.c | 33 +++++---- src/sdl/main.c | 97 +++++++++++++++++++++++++ src/soft/render.c | 2 +- src/soft/vga.c | 177 ++++++++++++++-------------------------------- src/soft/vga.h | 3 - 5 files changed, 171 insertions(+), 141 deletions(-) diff --git a/src/gl/render.c b/src/gl/render.c index 91707ef..fe0ec3c 100644 --- a/src/gl/render.c +++ b/src/gl/render.c @@ -1,5 +1,6 @@ #include "glob.h" #include "render.h" +#include "system.h" #include "files.h" #include "memory.h" #include "misc.h" @@ -26,9 +27,10 @@ #else # include #endif -#include // malloc free abs -#include // assert -#include +#include +#include +#include +#include #define VGA_TRANSPARENT_COLOR 0 #define DEFAULT_SKY_COLOR 0x97 @@ -80,7 +82,6 @@ int SCRW = 320; // public int SCRH = 200; // public static int gamma; static int fullscreen; -static SDL_Surface *surf; static rgb playpal[256]; static byte bright[256]; static GLuint lastTexture; @@ -1440,7 +1441,7 @@ void R_draw (void) { break; } GM_draw(); - SDL_GL_SwapBuffers(); + Y_swap_buffers(); } void R_alloc (void) { @@ -1670,19 +1671,22 @@ void R_alloc (void) { } void R_init (void) { - Uint32 flags = SDL_OPENGL; + int res = 0; + int flags = SYSTEM_USE_OPENGL; if (fullscreen) { - flags = flags | SDL_FULLSCREEN; + flags |= SYSTEM_USE_FULLSCREEN; } + logo("R_init: intialize opengl render\n"); + int was = Y_videomode_setted(); if (SCRW <= 0 || SCRH <= 0) { ERR_failinit("Invalid screen size %ix%i\n", SCRW, SCRH); } - if (surf == NULL) { + if (was == 0) { R_init_playpal(); // only onece } - surf = SDL_SetVideoMode(SCRW, SCRH, 0, flags); - if (surf == NULL) { - ERR_failinit("Unable to set video mode: %s\n", SDL_GetError()); + res = Y_set_videomode(SCRW, SCRH, flags); + if (res == 0) { + ERR_failinit("Unable to set video mode\n"); } root = R_cache_new(); assert(root); @@ -1691,6 +1695,7 @@ void R_init (void) { void R_done (void) { R_cache_free(root, 1); + Y_unset_videomode(); } void R_setgamma (int g) { @@ -1703,9 +1708,9 @@ int R_getgamma (void) { void R_toggle_fullscreen (void) { fullscreen = !fullscreen; - if (surf) { - R_init(); // recreate window - } +// if (surf) { +// R_init(); // recreate window +// } } void R_get_name (int n, char s[8]) { diff --git a/src/sdl/main.c b/src/sdl/main.c index 15ce855..5706cc7 100644 --- a/src/sdl/main.c +++ b/src/sdl/main.c @@ -25,6 +25,8 @@ #include #include // srand exit #include // strcasecmp +#include +#include "system.h" #include "input.h" #include "my.h" // fexists @@ -41,6 +43,9 @@ #include "render.h" // R_init R_draw R_done static int quit = 0; +static SDL_Surface *surf = NULL; + +/* --- error.h --- */ void logo (const char *s, ...) { va_list ap; @@ -84,6 +89,98 @@ void ERR_quit (void) { quit = 1; } +/* --- system.h --- */ + +int Y_set_videomode (int w, int h, int flags) { + SDL_Surface *s; + int colors; + Uint32 f; + assert(w > 0); + assert(h > 0); + f = SDL_DOUBLEBUF; + if (flags & SYSTEM_USE_FULLSCREEN) { + f = flags | SDL_FULLSCREEN; + } + if (flags & SYSTEM_USE_OPENGL) { + f = flags | SDL_OPENGL; + colors = 0; + } else { + f = flags | SDL_SWSURFACE | SDL_HWPALETTE; + colors = 8; + } + s = SDL_SetVideoMode(w, h, colors, f); + if (s != NULL) { + surf = s; + } + return s != NULL; +} + +int Y_videomode_setted (void) { + return surf != NULL; +} + +void Y_unset_videomode (void) { + surf = NULL; + SDL_QuitSubSystem(SDL_INIT_VIDEO); + SDL_InitSubSystem(SDL_INIT_VIDEO); +} + +int Y_set_fullscreen (int yes) { + //SDL_WM_ToggleFullScreen(); + return 0; +} + +int Y_get_fullscreen (void) { + return (surf != NULL) && ((surf->flags & SDL_FULLSCREEN) != 0); +} + +void Y_swap_buffers (void) { + assert(surf != NULL); + assert(surf->flags & SDL_OPENGL); + SDL_GL_SwapBuffers(); +} + +void Y_get_buffer (byte **buf, int *w, int *h, int *pitch) { + assert(surf != NULL); + assert((surf->flags & SDL_OPENGL) == 0); + *buf = surf->pixels; + *w = surf->w; + *h = surf->h; + *pitch = surf->pitch; +} + +void Y_set_vga_palette (byte *vgapal) { + int i; + byte *p = vgapal; + assert(vgapal != NULL); + assert(surf != NULL); + assert((surf->flags & SDL_OPENGL) == 0); + SDL_Color colors[256]; + for (i = 0; i < 256; i++) { + colors[i] = (SDL_Color) { + .r = p[0] * 255 / 63, + .g = p[1] * 255 / 63, + .b = p[2] * 255 / 63 + }; + p += 3; + } + SDL_SetColors(surf, colors, 0, 256); +} + +void Y_repaint_rect (int x, int y, int w, int h) { + assert(surf != NULL); + assert((surf->flags & SDL_OPENGL) == 0); + SDL_UpdateRect(surf, x, y, w, h); +} + +void Y_repaint (void) { + assert(surf != NULL); + assert((surf->flags & SDL_OPENGL) == 0); + SDL_Flip(surf); +} + +/* --- main --- */ + static int sdl_to_key (int code) { switch (code) { case SDLK_0: return KEY_0; diff --git a/src/soft/render.c b/src/soft/render.c index 2036b08..224d926 100644 --- a/src/soft/render.c +++ b/src/soft/render.c @@ -1342,7 +1342,7 @@ void R_setgamma(int g) { std_pal[t][1]=gamcor[gammaa][main_pal[t][1]]; std_pal[t][2]=gamcor[gammaa][main_pal[t][2]]; } - VP_setall(std_pal); + Y_set_vga_palette(std_pal); } int R_getgamma (void) { diff --git a/src/soft/vga.c b/src/soft/vga.c index 8347b3a..ef97ca7 100644 --- a/src/soft/vga.c +++ b/src/soft/vga.c @@ -22,22 +22,16 @@ #include "glob.h" #include "vga.h" -#include #include "error.h" #include "view.h" #include "memory.h" #include "misc.h" #include "files.h" +#include "system.h" +#include #include - -// адрес экранного буфера -//unsigned char *scra; - -// виртуальный экран -//unsigned char scrbuf[64000]; - int SCRW = 800; int SCRH = 600; char fullscreen = OFF; @@ -46,15 +40,13 @@ byte bright[256]; byte mixmap[256][256]; byte clrmap[256*12]; -static SDL_Surface* screen = NULL; -static int cx1,cx2,cy1,cy2; +static byte *buffer; +static int buf_w, buf_h, pitch; +static int offx, offy; +static int cx1, cx2, cy1, cy2; static byte flametab[16] = { 0xBC,0xBA,0xB8,0xB6,0xB4,0xB2,0xB0,0xD5,0xD6,0xD7,0xA1,0xA0,0xE3,0xE2,0xE1,0xE0 }; -static int offx = 0; -static int offy = 0; - -#define HQ 2 vgaimg *V_getvgaimg (int id) { int loaded = M_was_locked(id); @@ -73,83 +65,68 @@ vgaimg *V_loadvgaimg (char *name) { } short V_init (void) { - Uint32 flags = SDL_SWSURFACE|SDL_DOUBLEBUF|SDL_HWPALETTE; - if (fullscreen) flags = flags | SDL_FULLSCREEN; - screen = SDL_SetVideoMode(SCRW, SCRH, 8, flags); - if (!screen) ERR_failinit("Unable to set video mode: %s\n", SDL_GetError()); - SCRW /= HQ; - SCRH /= HQ; - return 0; + int flags = fullscreen ? SYSTEM_USE_FULLSCREEN : 0; + int res = Y_set_videomode(SCRW, SCRH, flags); + if (res == 0) { + ERR_failinit("Unable to set video mode"); + } + Y_get_buffer(&buffer, &buf_w, &buf_h, &pitch); + return 0; } -// переключение в текстовый режим void V_done (void) { - SDL_Quit(); + buffer = NULL; + buf_w = 0; + buf_h = 0; + pitch = 0; + Y_unset_videomode(); } static void draw_rect (int x, int y, int w, int h, int c) { - SDL_Rect dstrect; - dstrect.x = x*HQ; - dstrect.y = y*HQ; - dstrect.w = w*HQ; - dstrect.h = h*HQ; - SDL_FillRect(screen, &dstrect, c); + int i; + int x0 = max(x, cx1); + int y0 = max(y, cy1); + int x1 = min(x + w - 1, cx2); + int y1 = min(y + h - 1, cy2); + int len = x1 - x0; + for (i = y0; i <= y1; i++) { + memset(&buffer[i * pitch + x0], c, len); + } } -// установить область вывода -void V_setrect (short x,short w,short y,short h) { - SDL_Rect r; - r.x=x*HQ; - r.y=y*HQ; - r.w=w*HQ; - r.h=h*HQ; - SDL_SetClipRect(screen, &r); - SDL_GetClipRect(screen, &r); - cx1 = x; - cx2 = x+w-1; - cy1 = y; - cy2 = y+h-1; - if (cx1<0) cx1=0; - if (cx2>=SCRW) cx2=SCRW-1; - if (cy1<0) cy1=0; - if (cy2>=SCRH) cy2=SCRH-1; +void V_setrect (short x, short w, short y, short h) { + cx1 = max(x, 0); + cx2 = min(x + w - 1, SCRW - 1); + cy1 = max(y, 0); + cy2 = min(y + h - 1, SCRH - 1); } -static void putpixel (int x, int y, Uint8 color) { - if(x>=cx1 && x<=cx2 && y>=cy1 && y<=cy2) { - x*=HQ; - y*=HQ; - Uint8 *p = (Uint8 *)screen->pixels + y*screen->pitch + x; - *p = color; - *(p+1) = color; - p += screen->pitch; - *p = color; - *(p+1) = color; - } +static void putpixel (int x, int y, byte color) { + if (x >= cx1 && x <= cx2 && y >= cy1 && y <= cy2) { + buffer[y * pitch + x] = color; + } } static byte getpixel (int x, int y) { - if(x>=cx1 && x<=cx2 && y>=cy1 && y<=cy2) { - x*=HQ; - y*=HQ; - return *((Uint8 *)screen->pixels + y*screen->pitch + x); - } - return 0; + return x >= cx1 && x <= cx2 && y >= cy1 && y <= cy2 ? buffer[y * pitch + x] : 0; } -static void mappixel (int x, int y, byte* cmap) { - byte c = getpixel(x,y); - putpixel(x,y,cmap[c]); +static void mappixel (int x, int y, byte *cmap) { + byte c = getpixel(x, y); + putpixel(x, y, cmap[c]); } void V_center (int f) { - if (f) V_offset(SCRW/2-320/2, SCRH/2-200/2); - else V_offset(0, 0); + if (f) { + V_offset(SCRW / 2 - 320 / 2, SCRH / 2 - 200 / 2); + } else { + V_offset(0, 0); + } } void V_offset (int ox, int oy) { - offx=ox; - offy=oy; + offx = ox; + offy = oy; } static void draw_spr (short x, short y, vgaimg *i, int d, int c) { @@ -164,8 +141,8 @@ static void draw_spr (short x, short y, vgaimg *i, int d, int c) { for (ly=0; lyh; ly++) { for(lx=0; lxw; lx++) { int rx,ry; - rx = (d & 1) ? (i->w-lx-1) : (rx=lx); - ry = (d & 2) ? (i->h-ly-1) : (ry=ly); + rx = (d & 1) ? (i->w-lx-1) : (lx); + ry = (d & 2) ? (i->h-ly-1) : (ly); if (*p) { byte t = *p; if (c) if (t>=0x70 && t<=0x7F) t=t-0x70+c; @@ -195,9 +172,8 @@ void V_manspr2(int x,int y,void *p, unsigned char c) { draw_spr(x, y, p, 1, c); } -// вывести точку цвета c в координатах (x,y) -void V_dot (short x,short y, unsigned char c) { - putpixel(x,y,c); +void V_dot (short x, short y, unsigned char c) { + putpixel(x, y, c); } void smoke_sprf (int x, int y, byte c) { @@ -243,36 +219,12 @@ void V_clr (short x, short w, short y, short h, unsigned char c) { draw_rect(x, y, w, h, c); } -// установить n цветов, начиная с f, из массива p -static void VP_set (void *p, short f, short n) { - byte *ptr = (byte*)p; - SDL_Color colors[256]; - int i; - for(i=f;iformat->palette->ncolors; - SDL_Color colors[256]; - int i; - for (i=0; iformat->palette->colors[i].r; - colors[i].g = screen->format->palette->colors[i].g; - colors[i].b = screen->format->palette->colors[i].b; - } - - Uint32 flags = screen->flags; - - SDL_FreeSurface(screen); - - screen = SDL_SetVideoMode(0, 0, 0, flags ^ SDL_FULLSCREEN); - if(screen == NULL) { - ERR_fatal("Unable to set video mode\n"); - exit(1); - } - - SDL_SetPalette(screen, SDL_LOGPAL|SDL_PHYSPAL, colors, 0, ncolors); - } + fullscreen = Y_set_fullscreen(!Y_get_fullscreen()); } diff --git a/src/soft/vga.h b/src/soft/vga.h index 44f1d48..ecc6334 100644 --- a/src/soft/vga.h +++ b/src/soft/vga.h @@ -90,9 +90,6 @@ void V_manspr2 (int x, int y, void *p, unsigned char c); // x-левая сторона,w-ширина,y-верх,h-высота void V_clr (short x, short w, short y, short h, unsigned char c); -// установить палитру из массива p -void VP_setall (void *p); - // установить область вывода void V_setrect (short x, short w, short y, short h); -- 2.29.2