SDL3 Form SIM_Display

This commit is contained in:
JiXieShi
2024-09-21 20:43:31 +08:00
parent fbc054d999
commit 6c9a999c71
14 changed files with 448 additions and 62 deletions

38
sim/sdl/sim_sdl.cpp Normal file
View File

@@ -0,0 +1,38 @@
#include "sim_sdl.h"
bool SIM_SDLInit(char *name, int width, int height, int scale, SIM_SDL3_t *sdl3) {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
SDL_Log("SDL_Init failed: %s", SDL_GetError());
return false;
}
sdl3->scale = scale;
sdl3->window = SDL_CreateWindow(name, width * scale, height * scale, SDL_WINDOW_RESIZABLE);
if (!sdl3->window) {
SDL_Log("Could not create a window: %s", SDL_GetError());
return false;
}
SDL_SetWindowPosition(sdl3->window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
sdl3->renderer = SDL_CreateRenderer(sdl3->window, nullptr);
if (!sdl3->renderer) {
SDL_Log("Create renderer failed: %s", SDL_GetError());
return false;
}
return true;
}
void SIM_SDL_Color_DrawPiexl(SIM_SDL3_t *sdl3, uint16_t x, uint16_t y, SIM_Color_t color) {
SDL_SetRenderDrawColor(sdl3->renderer, color.ch.red, color.ch.green, color.ch.blue, color.ch.alpha);
for (int y_ = y * sdl3->scale; y_ < (y * sdl3->scale) + sdl3->scale; ++y_) {
for (int x_ = x * sdl3->scale; x_ < (x * sdl3->scale) + sdl3->scale; ++x_) {
SDL_RenderPoint(sdl3->renderer, x_, y_);
}
}
SDL_RenderPresent(sdl3->renderer);
}
void SIM_SDL_Stop(SIM_SDL3_t *sdl3) {
SDL_DestroyRenderer(sdl3->renderer);
SDL_DestroyWindow(sdl3->window);
SDL_Quit();
}

36
sim/sdl/sim_sdl.h Normal file
View File

@@ -0,0 +1,36 @@
#ifndef HW_LIB_SIM_SDL_H
#define HW_LIB_SIM_SDL_H
#include "SDL3/SDL.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Event *event;
int scale;
} SIM_SDL3_t;
typedef union {
struct {
uint8_t blue;
uint8_t green;
uint8_t red;
uint8_t alpha;
} ch;
uint32_t full;
} SIM_Color_t;
bool SIM_SDLInit(char *name, int width, int height, int scale, SIM_SDL3_t *sdl3);
void SIM_SDL_Color_DrawPiexl(SIM_SDL3_t *sdl3, uint16_t x, uint16_t y, SIM_Color_t color);
void SIM_SDL_Stop(SIM_SDL3_t *sdl3);
#ifdef __cplusplus
}
#endif
#endif //HW_LIB_SIM_SDL_H