aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2023-02-23 15:14:36 +0100
committerlonkaars <loek@pipeframe.xyz>2023-02-23 15:14:36 +0100
commitccff002705fb2dea16b4a2b733801b8b3fdfde2b (patch)
tree6d4b628a4c4c14c709dc8fea718bbeccecc4a8f2
parent6e1cce415050ca82f8243b5ae5a8d1564f311ee8 (diff)
sdl2 demo
-rw-r--r--test/sdl2/.gitignore2
-rw-r--r--test/sdl2/main.c48
-rw-r--r--test/sdl2/makefile18
-rw-r--r--test/sdl2/readme.md6
4 files changed, 74 insertions, 0 deletions
diff --git a/test/sdl2/.gitignore b/test/sdl2/.gitignore
new file mode 100644
index 0000000..f0c9b81
--- /dev/null
+++ b/test/sdl2/.gitignore
@@ -0,0 +1,2 @@
+*.o
+main
diff --git a/test/sdl2/main.c b/test/sdl2/main.c
new file mode 100644
index 0000000..beddc3f
--- /dev/null
+++ b/test/sdl2/main.c
@@ -0,0 +1,48 @@
+#include <stdlib.h>
+#include <stdbool.h>
+#include <math.h>
+#include <SDL2/SDL.h>
+
+#define UPSCALE_FACTOR 3
+#define WIN_HEIGHT 240
+#define WIN_WIDTH 320
+#define FPS 60
+
+static void pixel(SDL_Renderer* r, unsigned px, unsigned py, unsigned cr, unsigned cg, unsigned cb) {
+ SDL_SetRenderDrawColor(r, cr, cg, cb, 255);
+ SDL_RenderFillRect(r, &(SDL_Rect) {.x = px * UPSCALE_FACTOR, .y = py * UPSCALE_FACTOR, .w = UPSCALE_FACTOR, .h = UPSCALE_FACTOR});
+}
+
+int main() {
+ SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
+ SDL_Window *window = SDL_CreateWindow("sdl2 test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIN_WIDTH * UPSCALE_FACTOR, WIN_HEIGHT * UPSCALE_FACTOR, SDL_WINDOW_SHOWN);
+ SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
+
+ SDL_Event e;
+ unsigned long frame = 0;
+ while (true) {
+ uint32_t start = SDL_GetTicks();
+ frame++;
+ while (SDL_PollEvent(&e)) if (e.type == SDL_QUIT) exit(0);
+
+ SDL_RenderClear(renderer);
+
+ for (unsigned i = 0; i < WIN_WIDTH; i++) {
+ for (unsigned j = 0; j < WIN_HEIGHT; j++) {
+ pixel(renderer, i, j, (unsigned)(sqrt(i * 20) + frame) % 255, (i * j + frame) % 255, (j * 20) % 255);
+ }
+ }
+
+ SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
+ SDL_RenderPresent(renderer);
+
+ uint32_t end = SDL_GetTicks();
+ int wait_for = 1000 / FPS - (end - start);
+ if (wait_for > 0) SDL_Delay(wait_for);
+ }
+
+ SDL_DestroyRenderer(renderer);
+ SDL_DestroyWindow(window);
+ SDL_Quit();
+ return 0;
+}
diff --git a/test/sdl2/makefile b/test/sdl2/makefile
new file mode 100644
index 0000000..4c26dc3
--- /dev/null
+++ b/test/sdl2/makefile
@@ -0,0 +1,18 @@
+CC = gcc
+LD = gcc
+RM = rm -f
+TARGET = main
+LFLAGS += -lSDL2 -lm
+OBJS += main.o
+
+all: $(TARGET)
+
+%.o: %.c
+ $(CC) -c $(CFLAGS) $< -o $@
+
+$(TARGET): $(OBJS)
+ $(LD) $^ $(LFLAGS) -o $@
+
+clean:
+ $(RM) $(TARGET) $(OBJS)
+
diff --git a/test/sdl2/readme.md b/test/sdl2/readme.md
new file mode 100644
index 0000000..940e315
--- /dev/null
+++ b/test/sdl2/readme.md
@@ -0,0 +1,6 @@
+# sdl2 test
+
+this is a simple POC that makes a window to draw PPU graphics to using SDL2.
+this code will be moved to the stm32/ppu directory, and the stm32's makefile
+will be edited to include a sim target for building the game for desktop
+debugging.