From a4614438629c66914a6b8ee9696fe85eb5f4c4b0 Mon Sep 17 00:00:00 2001 From: Ian C Date: Sun, 23 May 2021 20:58:26 +0000 Subject: Updates for SDL2 --- src/gfx.c | 257 +++++++++++++++++++++++++++++++------------------------------- 1 file changed, 129 insertions(+), 128 deletions(-) (limited to 'src/gfx.c') diff --git a/src/gfx.c b/src/gfx.c index ab7768c..46abbb0 100644 --- a/src/gfx.c +++ b/src/gfx.c @@ -33,12 +33,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[]=EZX81_GFX_H; -static const char ident_fh[]=EZX81_FONT_H; - /* ---------------------------------------- MACROS */ @@ -50,72 +48,45 @@ static const char ident_fh[]=EZX81_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; static void (*putpixel)(int x, int y, Uint32 col); +#define NO_BMPIX 10 -/* ---------------------------------------- PRIVATE FUNCTIONS -*/ - -/* Taken from SDL documentation -*/ -static void normal_putpixel(int x, int y, Uint32 pixel) +static struct { - int bpp; - Uint8 *p; - - bpp=surface->format->BytesPerPixel; - p=(Uint8 *)surface->pixels+y*surface->pitch+x*bpp; + Uint32 col; + int r,g,b; +} bmpix[NO_BMPIX]= +{ + {0, 0x00,0x00,0x00}, /* BLACK */ + {0, 0x00,0x00,0xff}, /* BLUE */ + {0, 0xff,0x00,0x00}, /* RED */ + {0, 0xff,0x00,0xff}, /* MAGENTA */ + {0, 0x00,0xff,0x00}, /* GREEN */ + {0, 0x00,0xff,0xff}, /* CYAN */ + {0, 0xff,0xff,0x00}, /* YELLOW */ + {0, 0xff,0xff,0xff}, /* WHITE */ + {0, 0x60,0x60,0x60}, /* GREY */ +}; - 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; +/* ---------------------------------------- PRIVATE FUNCTIONS +*/ - case 4: - *(Uint32 *)p=pixel; - break; - } +static void normal_putpixel(int x, int y, Uint32 pixel) +{ + pixels[x + y * GFX_WIDTH] = pixel; } @@ -128,7 +99,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; + } } @@ -210,7 +219,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) { @@ -224,16 +238,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; @@ -261,29 +265,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); } @@ -291,18 +273,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 { @@ -311,24 +288,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; } @@ -345,8 +316,6 @@ void GFXPrint(int x, int y, Uint32 col, const char *format, ...) p=buff; - LOCK; - while(*p) { for(sy=0;sy<8;sy++) @@ -364,8 +333,6 @@ void GFXPrint(int x, int y, Uint32 col, const char *format, ...) p++; x+=8; } - - UNLOCK; } @@ -383,8 +350,6 @@ void GFXPrintPaper(int x, int y, Uint32 col, Uint32 paper, p=buff; - LOCK; - while(*p) { for(sy=0;sy<8;sy++) @@ -404,8 +369,44 @@ void GFXPrintPaper(int x, int y, Uint32 col, Uint32 paper, p++; x+=8; } +} + + +void GFXBitmap(int x, int y, int w, int h, const unsigned char *data) +{ + int pix; + int px,py; + + pix=0; + px=0; + py=0; + + while(TRUE) + { + int i; + + i=*data++; + + if (i<0x80) + { + pix=i; + + BMPlot(x,y,&px,&py,w,h,pix); - UNLOCK; + if (py>=h) + break; + } + else + { + int f; + + for (f=0;f=h) + break; + } + } } -- cgit v1.2.3