Make the SDL window resizable

replace/debe0b68e8d24ae39dae814f796efa2ef00932d3
Fred 2020-10-31 18:40:11 +01:00 zatwierdzone przez Niccolò Izzo
rodzic 58cb6857a5
commit 9f9b76e9b5
1 zmienionych plików z 60 dodań i 61 usunięć

Wyświetl plik

@ -28,6 +28,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#undef main /* necessary to avoid conflicts with SDL_main */ #undef main /* necessary to avoid conflicts with SDL_main */
/* /*
@ -47,21 +48,21 @@
#define PIX_FMT_RGB565 #define PIX_FMT_RGB565
#endif #endif
SDL_Window *window; /* SDL window */ SDL_Renderer *renderer; /* SDL renderer */
SDL_Surface *renderSurface; /* SDL rendering surface */ SDL_Window *window; /* SDL window */
void *frameBuffer; /* Pointer to framebuffer */ SDL_Texture *displayTexture; /* SDL rendering surface */
bool inProgress; /* Flag to signal when rendering is in progress */ void *frameBuffer; /* Pointer to framebuffer */
bool inProgress; /* Flag to signal when rendering is in progress */
/** /**
* @internal * @internal
* Internal helper function which fetches pixel at position (x, y) from framebuffer * Internal helper function which fetches pixel at position (x, y) from framebuffer
* and returns it in SDL-compatible format, which is ARGB8888. * and returns it in SDL-compatible format, which is ARGB8888.
*/ */
uint32_t fetchPixelFromFb(unsigned int x, unsigned int y) uint32_t fetchPixelFromFb(unsigned int x, unsigned int y) {
{
uint32_t pixel = 0; uint32_t pixel = 0;
#ifdef PIX_FMT_BW #ifdef PIX_FMT_BW
/* /*
* Black and white 1bpp format: framebuffer is an array of uint8_t, where * Black and white 1bpp format: framebuffer is an array of uint8_t, where
* each cell contains the values of eight pixels, one per bit. * each cell contains the values of eight pixels, one per bit.
@ -70,9 +71,9 @@ uint32_t fetchPixelFromFb(unsigned int x, unsigned int y)
unsigned int cell = (x + y*SCREEN_WIDTH) / 8; unsigned int cell = (x + y*SCREEN_WIDTH) / 8;
unsigned int elem = (x + y*SCREEN_WIDTH) % 8; unsigned int elem = (x + y*SCREEN_WIDTH) % 8;
if(fb[cell] & (1 << elem)) pixel = 0xFFFFFFFF; if(fb[cell] & (1 << elem)) pixel = 0xFFFFFFFF;
#endif #endif
#ifdef PIX_FMT_GRAYSC #ifdef PIX_FMT_GRAYSC
/* /*
* Convert from 8bpp grayscale to ARGB8888, we have to do nothing more that * Convert from 8bpp grayscale to ARGB8888, we have to do nothing more that
* replicating the pixel value for the three components * replicating the pixel value for the three components
@ -81,19 +82,19 @@ uint32_t fetchPixelFromFb(unsigned int x, unsigned int y)
uint8_t px = fb[x + y*SCREEN_WIDTH]; uint8_t px = fb[x + y*SCREEN_WIDTH];
pixel = 0xFF000000 | (px << 16) | (px << 8) | px; pixel = 0xFF000000 | (px << 16) | (px << 8) | px;
#endif #endif
#ifdef PIX_FMT_RGB565 #ifdef PIX_FMT_RGB565
/* /*
* SDL pixel format is ARGB8888, while ours is RGB565, thus we need * SDL pixel format is ARGB8888, while ours is RGB565, thus we need
* to do some conversions when writing framebuffer content to the * to do some conversions when writing framebuffer content to the
* window. We also set alpha value to its maximum. * window. We also set alpha value to its maximum.
*/ */
uint16_t *fb = (uint16_t *)(frameBuffer); uint16_t *fb = (uint16_t *) (frameBuffer);
uint32_t r = (fb[x + y*SCREEN_WIDTH] & 0xF800) >> 11; uint32_t r = (fb[x + y * SCREEN_WIDTH] & 0xF800) >> 11;
uint32_t g = (fb[x + y*SCREEN_WIDTH] & 0x07E0) >> 5; uint32_t g = (fb[x + y * SCREEN_WIDTH] & 0x07E0) >> 5;
uint32_t b = (fb[x + y*SCREEN_WIDTH] & 0x001F) & 0x1F; uint32_t b = (fb[x + y * SCREEN_WIDTH] & 0x001F) & 0x1F;
/* /*
* Here we do conversions by multiplying by some scaling factors, * Here we do conversions by multiplying by some scaling factors,
@ -103,62 +104,63 @@ uint32_t fetchPixelFromFb(unsigned int x, unsigned int y)
* - five bit to eight bit: 8.226 * - five bit to eight bit: 8.226
* - six bit to eight bit: 4.0476 * - six bit to eight bit: 4.0476
*/ */
r = (r * 8) + (r * 226)/1000; r = (r * 8) + (r * 226) / 1000;
g = (g * 4) + (g * 476)/10000; g = (g * 4) + (g * 476) / 10000;
b = (b * 8) + (b * 226)/1000; b = (b * 8) + (b * 226) / 1000;
pixel = 0xFF000000 | (r << 16) | (g << 8) | b; pixel = 0xFF000000 | (r << 16) | (g << 8) | b;
#endif #endif
return pixel; return pixel;
} }
void display_init() void display_init() {
{ if (SDL_Init(SDL_INIT_VIDEO) < 0) {
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("SDL video init error!!\n"); printf("SDL video init error!!\n");
} } else {
else
{
window = SDL_CreateWindow(" ", window = SDL_CreateWindow("OpenRTX",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH,SCREEN_HEIGHT, SCREEN_WIDTH * 3, SCREEN_HEIGHT * 3,
SDL_WINDOW_SHOWN); SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
renderSurface = SDL_GetWindowSurface(window); renderer = SDL_CreateRenderer(window, -1, 0);
SDL_FillRect(renderSurface,NULL,0xFFFFFF); SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
displayTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING,
SCREEN_WIDTH, SCREEN_HEIGHT);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, displayTexture, NULL, NULL);
SDL_RenderPresent(renderer);
/* /*
* Black and white pixel format: framebuffer type is uint8_t where each * Black and white pixel format: framebuffer type is uint8_t where each
* bit represents a pixel. We have to allocate * bit represents a pixel. We have to allocate
* (SCREEN_HEIGHT * SCREEN_WIDTH)/8 elements * (SCREEN_HEIGHT * SCREEN_WIDTH)/8 elements
*/ */
#ifdef PIX_FMT_BW #ifdef PIX_FMT_BW
unsigned int fbSize = (SCREEN_HEIGHT * SCREEN_WIDTH)/8; unsigned int fbSize = (SCREEN_HEIGHT * SCREEN_WIDTH)/8;
if((fbSize * 8) < (SCREEN_HEIGHT * SCREEN_WIDTH)) fbSize += 1; /* Compensate for eventual truncation error in division */ if((fbSize * 8) < (SCREEN_HEIGHT * SCREEN_WIDTH)) fbSize += 1; /* Compensate for eventual truncation error in division */
fbSize *= sizeof(uint8_t); fbSize *= sizeof(uint8_t);
#endif #endif
/* /*
* Grayscale pixel format: framebuffer type is uint8_t where each element * Grayscale pixel format: framebuffer type is uint8_t where each element
* controls one pixel * controls one pixel
*/ */
#ifdef PIX_FMT_GRAYSC #ifdef PIX_FMT_GRAYSC
unsigned int fbSize = SCREEN_HEIGHT * SCREEN_WIDTH * sizeof(uint8_t); unsigned int fbSize = SCREEN_HEIGHT * SCREEN_WIDTH * sizeof(uint8_t);
#endif #endif
/* /*
* RGB565 pixel format: framebuffer type is uint16_t where each element * RGB565 pixel format: framebuffer type is uint16_t where each element
* controls one pixel * controls one pixel
*/ */
#ifdef PIX_FMT_RGB565 #ifdef PIX_FMT_RGB565
unsigned int fbSize = SCREEN_HEIGHT * SCREEN_WIDTH * sizeof(uint16_t); unsigned int fbSize = SCREEN_HEIGHT * SCREEN_WIDTH * sizeof(uint16_t);
#endif #endif
frameBuffer = malloc(fbSize); frameBuffer = malloc(fbSize);
memset(frameBuffer, 0xFFFF, fbSize); memset(frameBuffer, 0xFFFF, fbSize);
@ -166,43 +168,40 @@ void display_init()
} }
} }
void display_terminate() void display_terminate() {
{ while (inProgress) {} /* Wait until current render finishes */
while(inProgress) { } /* Wait until current render finishes */
printf("Terminating SDL display emulator, goodbye!\n"); printf("Terminating SDL display emulator, goodbye!\n");
free(frameBuffer); free(frameBuffer);
SDL_DestroyWindow(window); SDL_DestroyWindow(window);
SDL_Quit(); SDL_Quit();
} }
void display_renderRows(uint8_t startRow, uint8_t endRow) void display_renderRows(uint8_t startRow, uint8_t endRow) {
{ Uint32 *pixels;
Uint32 *pixels = (Uint32*)renderSurface->pixels; int pitch = 0;
if (SDL_LockTexture(displayTexture, NULL, (void **) &pixels, &pitch) < 0) {
printf("SDL_lock failed: %s\n", SDL_GetError());
}
inProgress = true; inProgress = true;
for (unsigned int x = 0; x < SCREEN_WIDTH; x++) {
for(unsigned int x = 0; x < SCREEN_WIDTH; x++) for (unsigned int y = startRow; y < endRow; y++) {
{ pixels[x + y * SCREEN_WIDTH] = fetchPixelFromFb(x, y);
for(unsigned int y = startRow; y < endRow; y++)
{
pixels[x + y*SCREEN_WIDTH] = fetchPixelFromFb(x, y);
} }
} }
SDL_UnlockTexture(displayTexture);
SDL_RenderCopy(renderer, displayTexture, NULL, NULL);
SDL_RenderPresent(renderer);
inProgress = false; inProgress = false;
SDL_UpdateWindowSurface(window);
} }
void display_render() void display_render() {
{
display_renderRows(0, SCREEN_HEIGHT); display_renderRows(0, SCREEN_HEIGHT);
} }
bool display_renderingInProgress() bool display_renderingInProgress() {
{
return inProgress; return inProgress;
} }
void *display_getFrameBuffer() void *display_getFrameBuffer() {
{ return (void *) (frameBuffer);
return (void *)(frameBuffer);
} }