about summary refs log tree commit diff
diff options
context:
space:
mode:
authorManuel Palenzuela <manuelpalenzuelamerino@gmail.com>2021-07-19 18:26:09 +0100
committerManuel Palenzuela <manuelpalenzuelamerino@gmail.com>2021-07-19 18:26:09 +0100
commit34858842acf9a86a32003f7ed256192064a994ed (patch)
tree92e7a5521dd20027477f7420eb3e079580117f0d
parentFeature: Added timers thread (diff)
downloadCHIP8-Emulator-34858842acf9a86a32003f7ed256192064a994ed.tar.gz
CHIP8-Emulator-34858842acf9a86a32003f7ed256192064a994ed.tar.bz2
CHIP8-Emulator-34858842acf9a86a32003f7ed256192064a994ed.zip
Feature: Prepared SDL window for pixel drawing instruction
-rw-r--r--Makefile2
-rw-r--r--config.h1
-rw-r--r--emulator.c1
-rw-r--r--emulator.h1
-rw-r--r--main.c38
5 files changed, 42 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index 2b88fe9..2bc39b7 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
 CC=egcc
-CFLAGS=-std=c99 -g -Wall -I. -lpthread
+CFLAGS=-std=c99 -g -Wall -I. -lpthread -lSDL2
 
 all: chip8_emulator
 
diff --git a/config.h b/config.h
index a0d6073..a40e76f 100644
--- a/config.h
+++ b/config.h
@@ -6,6 +6,7 @@
 #define INSTRUCTIONS_PER_SECOND 700
 #define GAME_LOAD_LOCATION 0x200
 #define FONT_LOAD_LOCATION 0x50
+#define PIXEL_RATIO 10
 
 
 #define dbgprintf(fmt, ...) \
diff --git a/emulator.c b/emulator.c
index ce607fe..87b6e81 100644
--- a/emulator.c
+++ b/emulator.c
@@ -124,6 +124,7 @@ int emulator_tick(Emulator* emulator)
             break;
         case 0xD:
             dbgprintf("DRAW!\n");
+            emulator->draw_flag = 1;
             break;
         case 0xE:
             break;
diff --git a/emulator.h b/emulator.h
index cf1e81c..7a9c5ef 100644
--- a/emulator.h
+++ b/emulator.h
@@ -44,6 +44,7 @@ typedef struct
 typedef struct
 {
     volatile uint8_t is_on;
+    volatile uint8_t draw_flag;
     volatile uint8_t delay_timer;
     volatile uint8_t sound_timer;
     uint8_t display[64][32];
diff --git a/main.c b/main.c
index a257c96..3e4773d 100644
--- a/main.c
+++ b/main.c
@@ -1,4 +1,5 @@
 #include <emulator.h>
+#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;
 }