commit 2989d10faa5c40c07ac366d0ea06ec1bc9659e01
parent 68dc445f7d94e7222ba9fb4dc7f73c5c02d65436
Author: Léo Villeveygoux <leo.villeveygoux@etu.u-bordeaux.fr>
Date: Fri, 16 Jun 2017 18:42:06 +0200
add platform media support with SDL
Diffstat:
M | Makefile | | | 3 | +++ |
M | b8.c | | | 24 | +++++++++++++++++------- |
A | media.c | | | 159 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | media.h | | | 10 | ++++++++++ |
A | mem.h | | | 14 | ++++++++++++++ |
5 files changed, 203 insertions(+), 7 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,5 +1,6 @@
CFLAGS=-Wall -Wextra
LDFLAGS=-Wl,--section-start=mem_section=0x20000
+LDLIBS=-lSDL2
PROG=b8
CARTS=$(patsubst %.asm,%.bin,$(wildcard *.asm))
@@ -9,6 +10,8 @@ all: $(PROG) $(CARTS)
clean:
-$(RM) $(PROG) $(CARTS)
+$(PROG): media.c
+
%.bin: %.asm
yasm -f bin $< -o $@
chmod +x $@
diff --git a/b8.c b/b8.c
@@ -5,6 +5,9 @@
#include <stdlib.h>
#include <stdio.h>
+#include "mem.h"
+#include "media.h"
+
#define CHECK_STD_ERROR(predicate, name) do{\
if(predicate){\
perror(name);\
@@ -12,11 +15,7 @@
}\
}while(0)
-#define MEM_SIZE 0x40000
-#define CART_SIZE 0x20000
-#define CART_HEADER_SIZE 0x20
-
-char mem[MEM_SIZE] __attribute__ ((section ("mem_section"))) = {0};
+unsigned char mem[MEM_SIZE] __attribute__ ((section ("mem_section"))) = {0};
void load_cart(const char *filename){
int fd = open(filename, O_RDONLY);
@@ -41,8 +40,19 @@ int main(int argc, char *argv[]){
cart_fun = (void*)mem+CART_HEADER_SIZE;
- cart_fun();
+ load_media(argv[1]);
+
+ int cont = 1;
+ while(cont){
+ cont = update_input();
+
+ cart_fun();
+
+ update_graphics();
+ }
+
+ unload_media();
- printf("%d\n",(int)mem[CART_SIZE]);
+ printf("%d\n",(int)mem[SCREEN_MEM_OFFSET]);
return 0;
}
diff --git a/media.c b/media.c
@@ -0,0 +1,159 @@
+#include <stdlib.h>
+#include <stdio.h> //do I really need this?
+
+#include <SDL2/SDL.h>
+
+#include "mem.h"
+
+#include "media.h"
+
+#define SCREEN_WIDTH 256
+#define SCREEN_HEIGHT 256
+
+enum keys {
+ LEFT,
+ RIGHT,
+ UP,
+ DOWN,
+ X,
+ C,
+ RETURN,
+ ESCAPE,
+};
+
+SDL_Window *window;
+SDL_Renderer *renderer;
+SDL_Texture *texture;
+SDL_Rect rect = {.x=0,.y=0,.w=SCREEN_WIDTH,.h=SCREEN_HEIGHT};
+
+void load_media(const char *name){
+ //TODO: check every init
+ SDL_Init( SDL_INIT_EVERYTHING );
+ window = SDL_CreateWindow(
+ name,
+ SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
+ SCREEN_WIDTH, SCREEN_HEIGHT,
+ 0);
+
+ renderer = SDL_CreateRenderer(
+ window, -1,
+ SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
+
+ texture = SDL_CreateTexture(
+ renderer,
+ SDL_PIXELFORMAT_RGB332, SDL_TEXTUREACCESS_STREAMING,
+ SCREEN_WIDTH, SCREEN_HEIGHT);
+
+ SDL_RenderClear(renderer);
+}
+
+void unload_media(){
+ SDL_DestroyTexture(texture);
+ SDL_DestroyRenderer(renderer);
+ SDL_DestroyWindow(window);
+ SDL_Quit();
+}
+
+void update_graphics(){
+ void *mem_pixels = mem+SCREEN_MEM_OFFSET;
+ void *texture_pixels;
+ int pitch; /* we don't actually use this, yolo */
+ SDL_LockTexture(texture, &rect, &texture_pixels, &pitch);
+ memcpy(texture_pixels, mem_pixels, SCREEN_MEM_SIZE);
+ SDL_UnlockTexture(texture);
+
+ SDL_RenderCopy(renderer, texture, &rect, &rect);
+ SDL_RenderPresent(renderer);
+ SDL_RenderClear(renderer);
+}
+
+int update_input(){
+ unsigned char *input = mem+INPUT_MEM_OFFSET;
+ SDL_Event e;
+
+ while (SDL_PollEvent(&e)) {
+ if (e.type == SDL_QUIT) {
+ return 0;
+ }
+ if (e.type == SDL_KEYDOWN) {
+ switch (e.key.keysym.sym) {
+ case SDLK_LEFT:
+ *input |= 1<<LEFT;
+ break;
+
+ case SDLK_RIGHT:
+ *input |= 1<<RIGHT;
+ break;
+
+ case SDLK_UP:
+ *input |= 1<<UP;
+ break;
+
+ case SDLK_DOWN:
+ *input |= 1<<DOWN;
+ break;
+
+ case SDLK_x:
+ *input |= 1<<X;
+ break;
+
+ case SDLK_c:
+ *input |= 1<<C;
+ break;
+
+ case SDLK_RETURN:
+ *input |= 1<<RETURN;
+ break;
+
+ case SDLK_ESCAPE:
+ *input |= 1<<ESCAPE;
+ break;
+
+ case SDLK_q:
+ return 0;
+ default:
+ break;
+
+ }
+ }
+
+ if (e.type == SDL_KEYUP) {
+ switch (e.key.keysym.sym) {
+ case SDLK_LEFT:
+ *input &= ~(1<<LEFT);
+ break;
+
+ case SDLK_RIGHT:
+ *input &= ~(1<<RIGHT);
+ break;
+
+ case SDLK_UP:
+ *input &= ~(1<<UP);
+ break;
+
+ case SDLK_DOWN:
+ *input &= ~(1<<DOWN);
+ break;
+
+ case SDLK_x:
+ *input &= ~(1<<X);
+ break;
+
+ case SDLK_c:
+ *input &= ~(1<<C);
+ break;
+
+ case SDLK_RETURN:
+ *input &= ~(1<<RETURN);
+ break;
+
+ case SDLK_ESCAPE:
+ *input &= ~(1<<ESCAPE);
+ break;
+ }
+ }
+
+ }
+
+ return 1;
+}
diff --git a/media.h b/media.h
@@ -0,0 +1,10 @@
+#ifndef MEDIA_H
+#define MEDIA_H
+
+void load_media(const char *name);
+void unload_media();
+
+void update_graphics();
+int update_input();
+
+#endif /* MEDIA_H */
diff --git a/mem.h b/mem.h
@@ -0,0 +1,14 @@
+#ifndef MEM_H
+#define MEM_H
+
+#define MEM_SIZE 0x40000
+#define CART_SIZE 0x20000
+#define CART_HEADER_SIZE 0x20
+#define SCREEN_MEM_SIZE 0x10000
+
+#define SCREEN_MEM_OFFSET CART_SIZE
+#define INPUT_MEM_OFFSET (SCREEN_MEM_OFFSET+SCREEN_MEM_SIZE)
+
+extern unsigned char mem[];
+
+#endif /* MEM_H */