From 15d2a8724930bfb3d87f20b286391d4133d42d98 Mon Sep 17 00:00:00 2001 From: Ian C Date: Sun, 23 May 2021 20:45:19 +0000 Subject: Updates for SDL2 --- src/gfx.c | 184 +++++++++++++++++--------------------------------------------- 1 file changed, 50 insertions(+), 134 deletions(-) (limited to 'src/gfx.c') diff --git a/src/gfx.c b/src/gfx.c index 4a79b70..cf343d8 100644 --- a/src/gfx.c +++ b/src/gfx.c @@ -23,8 +23,6 @@ Wrapper to SDL */ -static const char ident[]="$Id$"; - #include #include #include @@ -33,12 +31,10 @@ static const char ident[]="$Id$"; #include "gfx.h" #include "exit.h" #include "config.h" +#include "util.h" #include "font.h" -static const char ident_h[]=ESPEC_GFX_H; -static const char ident_fh[]=ESPEC_FONT_H; - /* ---------------------------------------- MACROS */ @@ -50,24 +46,12 @@ static const char ident_fh[]=ESPEC_FONT_H; #define FALSE 0 #endif -#define LOCK do \ - { \ - if (SDL_MUSTLOCK(surface)) \ - if (SDL_LockSurface(surface)<0) \ - Exit("Failed to lock surface: %s\n", \ - SDL_GetError()); \ - } while(0) - -#define UNLOCK do \ - { \ - if (SDL_MUSTLOCK(surface)) \ - SDL_UnlockSurface(surface); \ - } while(0) - - /* ---------------------------------------- STATICS */ -static SDL_Surface *surface; +static SDL_Window *window; +static SDL_Renderer *renderer; +static SDL_Texture *texture; +static Uint32 *pixels; static Uint32 ticks; static Uint32 frame; static int scale; @@ -98,44 +82,9 @@ static struct /* ---------------------------------------- PRIVATE FUNCTIONS */ -/* Taken from SDL documentation -*/ static void normal_putpixel(int x, int y, Uint32 pixel) { - int bpp; - Uint8 *p; - - bpp=surface->format->BytesPerPixel; - p=(Uint8 *)surface->pixels+y*surface->pitch+x*bpp; - - switch(bpp) - { - case 1: - *p=pixel; - break; - - case 2: - *(Uint16 *)p=pixel; - break; - - case 3: - if(SDL_BYTEORDER==SDL_BIG_ENDIAN) - { - p[0]=(pixel>>16)&0xff; - p[1]=(pixel>>8)&0xff; - p[2]=pixel&0xff; - } else - { - p[0]=pixel&0xff; - p[1]=(pixel>>8)&0xff; - p[2]=(pixel>>16)&0xff; - } - break; - - case 4: - *(Uint32 *)p=pixel; - break; - } + pixels[x + y * GFX_WIDTH] = pixel; } @@ -148,7 +97,7 @@ static void scale_putpixel(int x, int y, Uint32 pixel) for(sx=0;sxformat,r,g,b); + return 0xff << 24 | (Uint32)r << 16 | (Uint32)g << 8 | b; } void GFXClear(Uint32 col) { - SDL_FillRect(surface,NULL,col); + int f; + + for(f = 0; f < GFX_WIDTH * scale * GFX_HEIGHT * scale; f++) + { + pixels[f] = col; + } } @@ -260,7 +226,12 @@ void GFXStartFrame(void) void GFXEndFrame(int delay) { - SDL_UpdateRect(surface,0,0,0,0); + SDL_UpdateTexture(texture, NULL, + pixels, GFX_WIDTH * scale * sizeof(Uint32)); + + SDL_RenderClear(renderer); + SDL_RenderCopy(renderer, texture, NULL, NULL); + SDL_RenderPresent(renderer); if (delay) { @@ -274,16 +245,6 @@ void GFXEndFrame(int delay) } -void GFXKeyRepeat(int repeat) -{ - if (repeat) - SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, - SDL_DEFAULT_REPEAT_INTERVAL); - else - SDL_EnableKeyRepeat(0,0); -} - - SDL_Event *GFXGetKey(void) { static SDL_Event e; @@ -311,29 +272,7 @@ SDL_Event *GFXWaitKey(void) } -void GFXLock(void) -{ - LOCK; -} - - -void GFXUnlock(void) -{ - UNLOCK; -} - - void GFXPlot(int x, int y, Uint32 col) -{ - LOCK; - - putpixel(x,y,col); - - UNLOCK; -} - - -void GFXFastPlot(int x, int y, Uint32 col) { putpixel(x,y,col); } @@ -341,18 +280,13 @@ void GFXFastPlot(int x, int y, Uint32 col) void GFXRect(int x, int y, int w, int h, Uint32 col, int solid) { - LOCK; - if (solid) { - SDL_Rect r; - - r.x=x*scale; - r.y=y*scale; - r.w=w*scale; - r.h=h*scale; - - SDL_FillRect(surface,&r,col); + while(h--) + { + DoHLine(x,x+w-1,y,col); + y++; + } } else { @@ -361,24 +295,18 @@ void GFXRect(int x, int y, int w, int h, Uint32 col, int solid) DoVLine(x,y,y+h-1,col); DoVLine(x+w-1,y,y+h-1,col); } - - UNLOCK; } void GFXHLine(int x1, int x2, int y, Uint32 col) { - LOCK; DoHLine(x1,x2,y,col); - UNLOCK; } void GFXVLine(int x, int y1, int y2, Uint32 col) { - LOCK; DoVLine(x,y1,y2,col); - UNLOCK; } @@ -395,8 +323,6 @@ void GFXPrint(int x, int y, Uint32 col, const char *format, ...) p=buff; - LOCK; - while(*p) { for(sy=0;sy<8;sy++) @@ -414,8 +340,6 @@ void GFXPrint(int x, int y, Uint32 col, const char *format, ...) p++; x+=8; } - - UNLOCK; } @@ -433,8 +357,6 @@ void GFXPrintPaper(int x, int y, Uint32 col, Uint32 paper, p=buff; - LOCK; - while(*p) { for(sy=0;sy<8;sy++) @@ -454,8 +376,6 @@ void GFXPrintPaper(int x, int y, Uint32 col, Uint32 paper, p++; x+=8; } - - UNLOCK; } @@ -464,8 +384,6 @@ void GFXBitmap(int x, int y, int w, int h, const unsigned char *data) int pix; int px,py; - LOCK; - pix=0; px=0; py=0; @@ -496,8 +414,6 @@ void GFXBitmap(int x, int y, int w, int h, const unsigned char *data) break; } } - - UNLOCK; } -- cgit v1.2.3