diff options
| author | 2019-09-28 02:22:28 -0400 | |
|---|---|---|
| committer | 2019-09-28 02:22:28 -0400 | |
| commit | 8da00c33383818955cf3166304756e927797f2c9 (patch) | |
| tree | 1f4122440b58caa08cd8bfb085966d46c413fa82 | |
| parent | bf544a28fb0b462f7dfe05f323bee1ee31878bc7 (diff) | |
| download | chip8-8da00c33383818955cf3166304756e927797f2c9.tar.gz chip8-8da00c33383818955cf3166304756e927797f2c9.tar.bz2 chip8-8da00c33383818955cf3166304756e927797f2c9.zip | |
sdl ui
| -rwxr-xr-x | Makefile | 10 | ||||
| -rw-r--r-- | chip8.c | 25 | ||||
| -rw-r--r-- | chip8.h | 2 | ||||
| -rw-r--r-- | ui.h | 8 | ||||
| -rw-r--r-- | ui_sdl.c | 77 |
5 files changed, 84 insertions, 38 deletions
@@ -1,7 +1,7 @@ all: CC=gcc win: CC=x86_64-w64-mingw32-gcc -CFLAGS = -O3 -Wall -std=c99 $(shell pkg-config --cflags ncursesw) +CFLAGS = -O3 -Wall -std=c99 $(shell pkg-config --cflags sdl2) all: BIN = chip8 win: BIN = chip8.exe @@ -9,11 +9,11 @@ win: BIN = chip8.exe all: executable win: executable -all: LDFLAGS = -lncursesw -ltinfow -lpthread -win: LDFLAGS = ./win/pdcurses.a -lpthread +all: LDFLAGS = -lSDL2 +win: LDFLAGS = -lSDL2 -executable: chip8.c ui.c - $(CC) $(CFLAGS) -o $(BIN) chip8.c ui.c $(LDFLAGS) +executable: chip8.c ui_sdl.c + $(CC) $(CFLAGS) -o $(BIN) chip8.c ui_sdl.c $(LDFLAGS) clean: $(RM) chip8 @@ -10,18 +10,13 @@ #include <window.h> #else #include <unistd.h> -#include <pthread.h> #endif -#include "ui.h" #include "util.h" #include "chip8.h" #define ROM_FILE "roms/tetris.ch8" -static void load_rom(); -static void init_vm(); - chip8_vm vm; void init_vm() { @@ -177,9 +172,6 @@ void vm_step() { if (pix) { int y = (vm.Vx[in>>4&0xF] + i)&0x1F; int x = (vm.Vx[in>>8&0xF] + s)&0x3F; - - //while (x > 0x3F) { x -= 0x3F; } - //while (y > 0x1F) { y -= 0x1F; } int byte = y * 64 + x; int bit = 1 << (byte & 0x07); @@ -247,20 +239,3 @@ void vm_step() { vm.PC += 0x2; } - -int main(void) { - srand(time(NULL)); - load_rom(); - init_vm(); - -#ifdef _WIN32 - HANDLE vm = CreateThread(NULL, 0, vm_thread, NULL, 0, NULL); -#else - //pthread_t vm; pthread_create(&vm, NULL, (void*)vm_thread, (void*)0); -#endif - - init_curses(); - curses_thread(0); - - return 0; -} @@ -2,7 +2,9 @@ #include "util.h" +void init_vm(); void vm_step(); +void load_rom(); typedef struct { u8* rom; @@ -1,8 +0,0 @@ -#pragma once - -#include <stdbool.h> -#include "util.h" - -void curses_thread(void* v); -u8 ui_get_key(bool block); -void init_curses(); diff --git a/ui_sdl.c b/ui_sdl.c new file mode 100644 index 0000000..250947c --- /dev/null +++ b/ui_sdl.c @@ -0,0 +1,77 @@ +#include <time.h> +#include <SDL2/SDL.h> + +#include "chip8.h" + +#define W 640 +#define H 320 + +SDL_Renderer *r; + +void setup() { + SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO); + SDL_Window* win = SDL_CreateWindow("chip8", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, W, H, SDL_WINDOW_SHOWN); + r = SDL_CreateRenderer(win, -1, SDL_RENDERER_PRESENTVSYNC|SDL_RENDERER_ACCELERATED); + SDL_RenderClear(r); +} + +void draw() { + SDL_Rect box = (SDL_Rect){1, 0, 10, 10}; + SDL_SetRenderDrawColor(r, 50, 0, 0, 255); + SDL_RenderClear(r); + SDL_SetRenderDrawColor(r, 160, 200, 255, 255); + //SDL_RenderFillRect(r, &box); + for (int x = 0; x < 0x3F; x++) { + for (int y = 0; y < 0x1F; y++) { + int byte = y * 64 + x; + int bit = byte & 0x07; + if ((vm.screen[byte>>3] & (1 << bit)) != 0) { + box.x = x * 10; box.y = y * 10; + //SDL_RenderFillRect(r, &box); + SDL_RenderDrawRect(r, &box); + } + } + } + SDL_RenderPresent(r); +} + +int main(void) { + srand(time(NULL)); + setup(); + load_rom(); + init_vm(); + + int last_60hz = SDL_GetTicks(); + + SDL_Event e; + while (1) { + SDL_PollEvent(&e); + switch (e.type) { + case SDL_KEYDOWN: + switch (e.key.keysym.sym) { + case 'q': + SDL_Quit(); + exit(0); + } + } + + int cur = SDL_GetTicks(); + if (cur - last_60hz >= (1000/60)) { + if (vm.DT > 0) { + vm.DT--; + } + + if (vm.ST > 0) { + vm.ST--; + } + + draw(); + last_60hz = cur; + } + + //SDL_Delay(1); + vm_step(); + } + return 0; +} + |