diff options
author | Ian C <ianc@noddybox.co.uk> | 2021-05-23 20:58:26 +0000 |
---|---|---|
committer | Ian C <ianc@noddybox.co.uk> | 2021-05-23 20:58:26 +0000 |
commit | a4614438629c66914a6b8ee9696fe85eb5f4c4b0 (patch) | |
tree | cf8c7f82d1683cefd0c3b9aba6760913d3baa43b /src/gfx.c | |
parent | 52d944a701d685ae8a91fe2f38485c4c61827d88 (diff) |
Diffstat (limited to 'src/gfx.c')
-rw-r--r-- | src/gfx.c | 257 |
1 files changed, 129 insertions, 128 deletions
@@ -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;sx<scale;sx++) for(sy=0;sy<scale;sy++) - normal_putpixel(x+sx,y+sy,pixel); + pixels[x+sx + (y+sy) * GFX_WIDTH * scale] = pixel; } @@ -146,10 +117,29 @@ static void DoVLine(int x, int y1, int y2, Uint32 col) } +static void BMPlot(int bx, int by, int *x, int *y, int w, int h, int col) +{ + if (*y<h) + { + putpixel(bx+*x,by+*y,bmpix[col].col); + + (*x)++; + + if (*x==w) + { + *x=0; + (*y)++; + } + } +} + + /* ---------------------------------------- EXPORTED INTERFACES */ void GFXInit(void) { + int f; + if (IConfig(CONF_FULLSCREEN)) scale=1; else @@ -166,39 +156,58 @@ void GFXInit(void) frame=1000/IConfig(CONF_FRAMES_PER_SEC); if (SDL_Init(SDL_INIT_TIMER|SDL_INIT_VIDEO)) - Exit("Failed to init SDL: %s\n",SDL_GetError()); + Exit("Failed to init SDL: %s\n",SDL_GetError()); + + if (!(window=SDL_CreateWindow("eSPEC", + SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, + GFX_WIDTH*scale, + GFX_HEIGHT*scale, + IConfig(CONF_FULLSCREEN) ? + SDL_WINDOW_FULLSCREEN : 0))) + { + Exit("Failed to open window: %s\n",SDL_GetError()); + } - if (!(surface=SDL_SetVideoMode(GFX_WIDTH*scale, - GFX_HEIGHT*scale, - 0, - IConfig(CONF_FULLSCREEN) ? - SDL_FULLSCREEN : 0))) + if (!(renderer = SDL_CreateRenderer(window, -1, 0))) { - Exit("Failed to open video: %s\n",SDL_GetError()); + Exit("Failed to create renderer: %s\n",SDL_GetError()); } - SDL_ShowCursor(SDL_DISABLE); - SDL_WM_SetCaption("eZX81","eZX81"); + if (!(texture = SDL_CreateTexture(renderer, + SDL_PIXELFORMAT_ARGB8888, + SDL_TEXTUREACCESS_STREAMING, + GFX_WIDTH*scale, + GFX_HEIGHT*scale))) + { + Exit("Failed to create texture: %s\n",SDL_GetError()); + } - atexit(SDL_Quit); -} + pixels = Malloc(sizeof(Uint32) * GFX_WIDTH*scale * GFX_HEIGHT*scale); + SDL_ShowCursor(SDL_DISABLE); -SDL_Surface *GFXGetSurface(void) -{ - return surface; + for(f=0;f<NO_BMPIX;f++) + bmpix[f].col=GFXRGB(bmpix[f].r,bmpix[f].g,bmpix[f].b); + + atexit(SDL_Quit); } Uint32 GFXRGB(Uint8 r, Uint8 g, Uint8 b) { - return SDL_MapRGB(surface->format,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,48 +265,21 @@ 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); } 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<i-0x80;f++) + BMPlot(x,y,&px,&py,w,h,pix); + + if (py>=h) + break; + } + } } |