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,8 +48,9 @@
#define PIX_FMT_RGB565 #define PIX_FMT_RGB565
#endif #endif
SDL_Renderer *renderer; /* SDL renderer */
SDL_Window *window; /* SDL window */ SDL_Window *window; /* SDL window */
SDL_Surface *renderSurface; /* SDL rendering surface */ SDL_Texture *displayTexture; /* SDL rendering surface */
void *frameBuffer; /* Pointer to framebuffer */ void *frameBuffer; /* Pointer to framebuffer */
bool inProgress; /* Flag to signal when rendering is in progress */ bool inProgress; /* Flag to signal when rendering is in progress */
@ -57,8 +59,7 @@ bool inProgress; /* Flag to signal when rendering is in progress */
* 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
@ -114,24 +115,25 @@ uint32_t fetchPixelFromFb(unsigned int x, unsigned int y)
} }
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
@ -166,8 +168,7 @@ 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);
@ -175,34 +176,32 @@ void display_terminate()
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++) {
{
for(unsigned int y = startRow; y < endRow; y++)
{
pixels[x + y * SCREEN_WIDTH] = fetchPixelFromFb(x, 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);
} }