b8

A x86 32bits native fantazy console.
git clone git://git.vgx.fr/b8
Log | Files | Refs

media.c (2688B)


      1 #include <stdlib.h>
      2 #include <stdio.h> //do I really need this?
      3 
      4 #include <SDL2/SDL.h>
      5 
      6 #include "mem.h"
      7 
      8 #include "media.h"
      9 
     10 #define SCREEN_WIDTH 256
     11 #define SCREEN_HEIGHT 256
     12 
     13 enum keys {
     14 	LEFT,
     15 	RIGHT,
     16 	UP,
     17 	DOWN,
     18 	X,
     19 	C,
     20 	RETURN,
     21 	ESCAPE,
     22 };
     23 
     24 SDL_Window *window;
     25 SDL_Renderer *renderer;
     26 SDL_Texture *texture;
     27 SDL_Rect rect = {.x=0,.y=0,.w=SCREEN_WIDTH,.h=SCREEN_HEIGHT};
     28 
     29 void load_media(const char *name){
     30 	//TODO: check every init
     31 	SDL_Init( SDL_INIT_EVERYTHING );
     32 	window = SDL_CreateWindow(
     33 			name,
     34 			SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
     35 			SCREEN_WIDTH, SCREEN_HEIGHT,
     36 			0);
     37 
     38 	renderer = SDL_CreateRenderer(
     39 			window, -1,
     40 			SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
     41 
     42 	texture = SDL_CreateTexture(
     43 			renderer,
     44 			SDL_PIXELFORMAT_RGB332, SDL_TEXTUREACCESS_STREAMING,
     45 			SCREEN_WIDTH, SCREEN_HEIGHT);
     46 
     47 	SDL_RenderClear(renderer);
     48 }
     49 
     50 void unload_media(){
     51 	SDL_DestroyTexture(texture);
     52 	SDL_DestroyRenderer(renderer);
     53 	SDL_DestroyWindow(window);
     54 	SDL_Quit();
     55 }
     56 
     57 void update_graphics(){
     58 	void *mem_pixels = mem+SCREEN_MEM_OFFSET;
     59 	void *texture_pixels;
     60 	int pitch; /* we don't actually use this, yolo */
     61 	SDL_LockTexture(texture, &rect, &texture_pixels, &pitch);
     62 	memcpy(texture_pixels, mem_pixels, SCREEN_MEM_SIZE);
     63 	SDL_UnlockTexture(texture);
     64 
     65 	SDL_RenderCopy(renderer, texture, &rect, &rect);
     66 	SDL_RenderPresent(renderer);
     67 	SDL_RenderClear(renderer);
     68 }
     69 
     70 int update_input(){
     71 	unsigned char *input = mem+INPUT_MEM_OFFSET;
     72 	SDL_Event e;
     73 
     74 	while (SDL_PollEvent(&e)) {
     75 		if (e.type == SDL_QUIT) {
     76 			return 0;
     77 		}
     78 		if (e.type == SDL_KEYDOWN) {
     79 			switch (e.key.keysym.sym) {
     80 				case SDLK_LEFT:
     81 					*input |= 1<<LEFT;
     82 					break;
     83 
     84 				case SDLK_RIGHT:
     85 					*input |= 1<<RIGHT;
     86 					break;
     87 
     88 				case SDLK_UP:
     89 					*input |= 1<<UP;
     90 					break;
     91 
     92 				case SDLK_DOWN:
     93 					*input |= 1<<DOWN;
     94 					break;
     95 
     96 				case SDLK_x:
     97 					*input |= 1<<X;
     98 					break;
     99 
    100 				case SDLK_c:
    101 					*input |= 1<<C;
    102 					break;
    103 
    104 				case SDLK_RETURN:
    105 					*input |= 1<<RETURN;
    106 					break;
    107 
    108 				case SDLK_ESCAPE:
    109 					*input |= 1<<ESCAPE;
    110 					break;
    111 
    112 				case SDLK_q:
    113 					return 0;
    114 				default:
    115 					break;
    116 
    117 			}
    118 		}
    119 
    120 		if (e.type == SDL_KEYUP) {
    121 			switch (e.key.keysym.sym) {
    122 				case SDLK_LEFT:
    123 					*input &= ~(1<<LEFT);
    124 					break;
    125 
    126 				case SDLK_RIGHT:
    127 					*input &= ~(1<<RIGHT);
    128 					break;
    129 
    130 				case SDLK_UP:
    131 					*input &= ~(1<<UP);
    132 					break;
    133 
    134 				case SDLK_DOWN:
    135 					*input &= ~(1<<DOWN);
    136 					break;
    137 
    138 				case SDLK_x:
    139 					*input &= ~(1<<X);
    140 					break;
    141 
    142 				case SDLK_c:
    143 					*input &= ~(1<<C);
    144 					break;
    145 
    146 				case SDLK_RETURN:
    147 					*input &= ~(1<<RETURN);
    148 					break;
    149 
    150 				case SDLK_ESCAPE:
    151 					*input &= ~(1<<ESCAPE);
    152 					break;
    153 			}
    154 		}
    155 
    156 	}
    157 
    158 	return 1;
    159 }