display framebuffer in sdl window

This commit is contained in:
2023-03-30 09:16:01 +02:00
parent aec48ce3f0
commit 3c6c9a3745
2 changed files with 40 additions and 6 deletions

View File

@@ -8,13 +8,13 @@ run-ts:
deno run --allow-read main.ts deno run --allow-read main.ts
wasm3-test: main.o $(WASM3_O) wasm3-test: main.o $(WASM3_O)
gcc -g -lm -o wasm3-test $^ gcc -g -lm -lSDL2 -o wasm3-test $^
wasm3/source/%.o: wasm3/source/%.c wasm3/source/%.o: wasm3/source/%.c
gcc -g -DDEBUG=1 -c -o $@ $^ gcc -g -O2 -c -o $@ $^
main.o: main.c main.o: main.c
gcc -g -DDEBUG=1 -c -o main.o main.c gcc -g -O2 -c -o main.o main.c
clean: clean:
rm wasm3-test main.o $(WASM3_O) rm wasm3-test main.o $(WASM3_O)

40
main.c
View File

@@ -1,5 +1,8 @@
#include "wasm3/source/wasm3.h" #include "wasm3/source/wasm3.h"
#include "wasm3/source/m3_env.h" #include "wasm3/source/m3_env.h"
#include "SDL2/SDL.h"
#include "SDL2/SDL_video.h"
#include "SDL2/SDL_render.h"
#include <math.h> #include <math.h>
#include <stdio.h> #include <stdio.h>
#include <malloc.h> #include <malloc.h>
@@ -174,9 +177,40 @@ int main() {
verifyM3(runtime, m3_CompileModule(cartMod)); verifyM3(runtime, m3_CompileModule(cartMod));
verifyM3(runtime, m3_RunStart(cartMod)); verifyM3(runtime, m3_RunStart(cartMod));
IM3Function updFunc; SDL_Init(SDL_INIT_VIDEO);
verifyM3(runtime, m3_FindFunction(&updFunc, runtime, "upd")); SDL_Window* window;
verifyM3(runtime, m3_CallV(updFunc)); SDL_Renderer* renderer;
SDL_CreateWindowAndRenderer(320, 240, SDL_WINDOW_RESIZABLE, &window, &renderer);
SDL_RenderSetLogicalSize(renderer, 320, 240);
SDL_Texture* texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, 320, 240);
uint32_t* pixels32 = malloc(320*240*4);
for(uint32_t time = 0;; time += 16) {
SDL_Event event;
while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_QUIT:
exit(0);
}
}
*(uint32_t*)(memory + 64) = time;
IM3Function updFunc;
verifyM3(runtime, m3_FindFunction(&updFunc, runtime, "upd"));
verifyM3(runtime, m3_CallV(updFunc));
uint32_t* palette = (uint32_t*)(memory + 0x13000);
uint8_t* pixels = memory + 120;
for(uint32_t i = 0; i < 320*240; ++i) {
pixels32[i] = palette[pixels[i]];
}
SDL_UpdateTexture(texture, NULL, pixels32, 320*4);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
}
return 0; return 0;
} }