commit a7483ca29f31bd9719e0ba0f512e2fb40ae8e5e3
Author: Léo Villeveygoux <leo.villeveygoux@etu.u-bordeaux.fr>
Date: Fri, 9 Jun 2017 19:37:39 +0200
initial commit
Diffstat:
A | Makefile | | | 6 | ++++++ |
A | clib.c | | | 30 | ++++++++++++++++++++++++++++++ |
A | core.c | | | 171 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
3 files changed, 207 insertions(+), 0 deletions(-)
diff --git a/Makefile b/Makefile
@@ -0,0 +1,6 @@
+LDLIBS ?= -lSDL2 -lSDL2_image
+CFLAGS ?= -Wall -Wextra
+
+all: test
+
+test: core.o clib.c
diff --git a/clib.c b/clib.c
@@ -0,0 +1,30 @@
+extern char pixels[256*256];
+extern char input;
+
+unsigned char x = 128, y = 128;
+
+unsigned iter;
+
+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(){
+ for(int i = 0 ; i < 256*256 ; i++){
+ pixels[i] = color(i);
+ }
+}
+
+void loop(){
+ if(input & 2)
+ x++;
+ if(input & 1)
+ x--;
+ if(input & 4)
+ y--;
+ if(input & 8)
+ y++;
+
+ pixels[x+y*256] = color(256*256-(x+y*256));
+}
+
diff --git a/core.c b/core.c
@@ -0,0 +1,171 @@
+#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;
+}