From 34858842acf9a86a32003f7ed256192064a994ed Mon Sep 17 00:00:00 2001 From: Manuel Palenzuela Date: Mon, 19 Jul 2021 18:26:09 +0100 Subject: Feature: Prepared SDL window for pixel drawing instruction --- main.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'main.c') diff --git a/main.c b/main.c index a257c96..3e4773d 100644 --- a/main.c +++ b/main.c @@ -1,4 +1,5 @@ #include +#include "/usr/local/include/SDL2/SDL.h" int main(int argc, char** argv); void show_help(); @@ -11,6 +12,12 @@ int main(int argc, char** argv) return 1; } + + SDL_Init(SDL_INIT_VIDEO); + SDL_Window * window = SDL_CreateWindow("CHIP8 Emulator", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 64 * PIXEL_RATIO, 32 * PIXEL_RATIO, 0); + SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0); + SDL_Texture * texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 64, 32); + Emulator emulator; emulator_initialise(&emulator); @@ -20,12 +27,43 @@ int main(int argc, char** argv) printf("Hello brother!\n"); + uint32_t pixels[32 * 64]; + for(int w = 0; w < 64; ++w) + for(int h = 0; h < 32; ++h) + { + uint8_t pixel = emulator.display[w][h]; + pixels[64 * h + w] = (0x00FFFFFF * pixel) | 0xFF000000; + } + + SDL_Event event; while(emulator.is_on == 1) { + while(SDL_PollEvent(&event)) + { + if(event.type == SDL_QUIT) + goto exit; + } + emulator_tick(&emulator); + + if(emulator.draw_flag == 1) + { + SDL_UpdateTexture(texture, NULL, pixels, sizeof(uint32_t) * 64); + SDL_RenderClear(renderer); + SDL_RenderCopy(renderer, texture, NULL, NULL); + SDL_RenderPresent(renderer); + emulator.draw_flag = 0; + } + usleep(1000000 / INSTRUCTIONS_PER_SECOND); } + exit: + SDL_DestroyTexture(texture); + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + SDL_Quit(); + return 0; } -- cgit 1.4.1