commit 74b6f0f881c61f3cf24c3d7cf940d444f38d1229
parent a7483ca29f31bd9719e0ba0f512e2fb40ae8e5e3
Author: Léo Villeveygoux <leo.villeveygoux@etu.u-bordeaux.fr>
Date: Fri, 9 Jun 2017 20:30:15 +0200
use dynamic modules
Diffstat:
M | Makefile | | | 16 | ++++++++++++---- |
M | clib.c | | | 17 | ++++++++++------- |
D | core.c | | | 171 | ------------------------------------------------------------------------------- |
A | ob.c | | | 193 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
4 files changed, 215 insertions(+), 182 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,6 +1,14 @@
-LDLIBS ?= -lSDL2 -lSDL2_image
-CFLAGS ?= -Wall -Wextra
+LDLIBS ?= -lSDL2 -lSDL2_image -ldl
+CFLAGS ?= -Wall -Wextra -fPIC
-all: test
+all: ob clib.so
-test: core.o clib.c
+%.so: %.o
+ $(CC) $(LDFLAGS) -shared -o $@ $^ $(LDLIBS)
+
+ob: ob.o
+
+clib.so:
+
+clean:
+ -$(RM) *.o *.so ob
diff --git a/clib.c b/clib.c
@@ -1,5 +1,5 @@
-extern char pixels[256*256];
-extern char input;
+unsigned char* pixels;
+unsigned char* input;
unsigned char x = 128, y = 128;
@@ -9,20 +9,23 @@ char color(int i){
return (char) ((i%128)*8/128)<<5 | (((i/256)*16/256)%8)<<2 | (i%256 < 128 ? 0:1) | (i/256 < 128 ? 0:2);
}
-void init(){
+void init(unsigned char* pixels_ptr, unsigned char* input_ptr){
+ pixels = pixels_ptr;
+ input = input_ptr;
+
for(int i = 0 ; i < 256*256 ; i++){
pixels[i] = color(i);
}
}
void loop(){
- if(input & 2)
+ if(*input & 2)
x++;
- if(input & 1)
+ if(*input & 1)
x--;
- if(input & 4)
+ if(*input & 4)
y--;
- if(input & 8)
+ if(*input & 8)
y++;
pixels[x+y*256] = color(256*256-(x+y*256));
diff --git a/core.c b/core.c
@@ -1,171 +0,0 @@
-#include <stdlib.h>
-#include <stdio.h>
-#include <SDL2/SDL.h>
-#include <SDL2/SDL_image.h>
-
-enum keys {
- LEFT,
- RIGHT,
- UP,
- DOWN,
- A,
- B,
- RETURN,
- ESCAPE,
-};
-
-/* api */
-extern void init();
-extern void loop();
-
-/* global data */
-char pixels[256*256];
-unsigned char input;
-
-void show_FPS(unsigned iter){
- static unsigned oldtime;
- static unsigned newtime;
-
- if(iter%64 == 0){
- oldtime = newtime;
- newtime = SDL_GetTicks();
- fprintf(stderr,"fps %.3f \r", 1000.0/((newtime-oldtime)/64.0));
- }
-}
-
-int get_events(void){
- SDL_Event e;
- while (SDL_PollEvent(&e)) {
- if (e.type == SDL_QUIT
- || (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_q)) {
- 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_a:
- input |= 1<<A;
- break;
-
- case SDLK_z:
- input |= 1<<B;
- break;
-
- case SDLK_RETURN:
- input |= 1<<RETURN;
- break;
-
- case SDLK_ESCAPE:
- input |= 1<<ESCAPE;
- 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_a:
- input &= ~(1<<A);
- break;
-
- case SDLK_z:
- input &= ~(1<<B);
- break;
-
- case SDLK_RETURN:
- input &= ~(1<<RETURN);
- break;
-
- case SDLK_ESCAPE:
- input &= ~(1<<ESCAPE);
- break;
- }
- }
-
- }
-
- return 1;
-}
-
-int main(){
- // init
- SDL_Init( SDL_INIT_EVERYTHING );
- SDL_Window *window = SDL_CreateWindow("test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 256, 256, 0);
- SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
- SDL_Texture * texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB332, SDL_TEXTUREACCESS_STREAMING, 256, 256);
-
- SDL_Rect rect = {.x=0,.y=0,.w=256,.h=256};
-
- init();
-
- void *pix;
- int pitch;
- SDL_RenderClear(renderer);
- SDL_LockTexture(texture, &rect, &pix, &pitch);
- memcpy(pix, pixels, 256*256);
- SDL_UnlockTexture(texture);
-
-
- puts("inited");
-
- // loop
- unsigned iter = 0;
- int cont = 1;
- while(cont) {
-// show_FPS(iter++);
-
- cont = get_events();
-
- fprintf(stderr,"input %3u \r", (unsigned) input);
-
- loop();
-
- SDL_LockTexture(texture, &rect, &pix, &pitch);
- memcpy(pix, pixels, 256*256);
- SDL_UnlockTexture(texture);
-
- SDL_RenderCopy(renderer, texture, &rect, &rect);
- SDL_RenderPresent(renderer);
- SDL_RenderClear(renderer);
- }
-
- // finish
- puts("\nfinish");
-
- SDL_DestroyTexture(texture);
- SDL_DestroyRenderer(renderer);
- SDL_DestroyWindow(window);
- SDL_Quit();
-
- return 0;
-}
diff --git a/ob.c b/ob.c
@@ -0,0 +1,193 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <dlfcn.h>
+
+#include <SDL2/SDL.h>
+#include <SDL2/SDL_image.h>
+
+#define CHECK_DLERROR(ptr) do\
+ if(!ptr){\
+ fprintf(stderr, "%s\n", dlerror());\
+ exit(1);\
+ } while (0)
+
+enum keys {
+ LEFT,
+ RIGHT,
+ UP,
+ DOWN,
+ A,
+ B,
+ RETURN,
+ ESCAPE,
+};
+
+/* global data */
+unsigned char pixels[256*256];
+unsigned char input;
+
+float FPS;
+
+void update_FPS(unsigned iter){
+ static unsigned oldtime;
+ static unsigned newtime;
+
+ if(iter%64 == 0){
+ oldtime = newtime;
+ newtime = SDL_GetTicks();
+ FPS = 1000.0/((newtime-oldtime)/64.0);
+ }
+}
+
+int get_events(void){
+ SDL_Event e;
+ while (SDL_PollEvent(&e)) {
+ if (e.type == SDL_QUIT
+ || (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_q)) {
+ 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_a:
+ input |= 1<<A;
+ break;
+
+ case SDLK_z:
+ input |= 1<<B;
+ break;
+
+ case SDLK_RETURN:
+ input |= 1<<RETURN;
+ break;
+
+ case SDLK_ESCAPE:
+ input |= 1<<ESCAPE;
+ 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_a:
+ input &= ~(1<<A);
+ break;
+
+ case SDLK_z:
+ input &= ~(1<<B);
+ break;
+
+ case SDLK_RETURN:
+ input &= ~(1<<RETURN);
+ break;
+
+ case SDLK_ESCAPE:
+ input &= ~(1<<ESCAPE);
+ break;
+ }
+ }
+
+ }
+
+ return 1;
+}
+
+int main(int argc, char *argv[]){
+ // init
+ SDL_Init( SDL_INIT_EVERYTHING );
+ SDL_Window *window = SDL_CreateWindow("test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 256, 256, 0);
+ SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
+ SDL_Texture * texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB332, SDL_TEXTUREACCESS_STREAMING, 256, 256);
+
+ SDL_Rect rect = {.x=0,.y=0,.w=256,.h=256};
+
+ if(argc != 2){
+ fprintf(stderr, "Usage: %s ./file.so\n", argv[0]);
+ exit(1);
+ }
+
+ void *handle = dlopen(argv[1], RTLD_LAZY);
+ CHECK_DLERROR(handle);
+
+ void (*init)(unsigned char*, unsigned char*) = dlsym(handle, "init");
+ CHECK_DLERROR(init);
+
+ void (*loop)() = dlsym(handle, "loop");
+ CHECK_DLERROR(loop);
+
+ init((unsigned char*)&pixels, &input);
+
+ void *pix;
+ int pitch;
+ SDL_RenderClear(renderer);
+ SDL_LockTexture(texture, &rect, &pix, &pitch);
+ memcpy(pix, pixels, 256*256);
+ SDL_UnlockTexture(texture);
+
+
+ puts("inited");
+
+ // loop
+ unsigned iter = 0;
+ int cont = 1;
+ while(cont) {
+ update_FPS(iter);
+
+ cont = get_events();
+
+ fprintf(stderr,"input %3u, FPS %3.3f\r", (unsigned) input, FPS);
+
+ loop();
+
+ SDL_LockTexture(texture, &rect, &pix, &pitch);
+ memcpy(pix, pixels, 256*256);
+ SDL_UnlockTexture(texture);
+
+ SDL_RenderCopy(renderer, texture, &rect, &rect);
+ SDL_RenderPresent(renderer);
+ SDL_RenderClear(renderer);
+
+ iter++;
+ }
+
+ // finish
+ puts("\nfinish");
+
+ SDL_DestroyTexture(texture);
+ SDL_DestroyRenderer(renderer);
+ SDL_DestroyWindow(window);
+ SDL_Quit();
+
+ return 0;
+}