SayoriOS  0.3.3
sdl.c
1 #include "common.h"
2 #include "drv/input/keymap.h"
3 #include "cpu.h"
4 #include "io/rgb_image.h"
5 #include "io/screen.h"
6 
7 static unsigned int frames;
8 
9 static int button_start, button_select;
10 static int button_a, button_b;
11 static int button_down, button_up, button_left, button_right;
12 static int button_quit;
13 
14 struct keymap {
15  char code;
16  int *key;
17  void (*f)(void);
18  int prev;
19 };
20 
21 static struct keymap keys[] =
22 {
23  {KEY_A, &button_a, NULL, 0},
24  {KEY_S, &button_b, NULL, 0},
25  {KEY_D, &button_select, NULL, 0},
26  {KEY_F, &button_start, NULL, 0},
27  {KEY_LEFT, &button_left, NULL, 0},
28  {KEY_RIGHT, &button_right, NULL, 0},
29  {KEY_UP, &button_up, NULL, 0},
30  {KEY_DOWN, &button_down, NULL, 0},
31  {KEY_ESC, &button_quit, NULL, 0}
32 };
33 
34 #define WIDTH 640
35 #define HEIGHT 576
36 
37 uint32_t* gb_framebuffer = 0;
38 
39 static size_t offsetx = 0;
40 static size_t offsety = 0;
41 
42 int sdl_init(void)
43 {
44  gb_framebuffer = kcalloc(WIDTH * HEIGHT, 4);
45 
46  offsetx = (getScreenWidth() - WIDTH) / 2;
47  offsety = (getScreenHeight() - HEIGHT) / 2;
48 
49  /*
50  SDL_Init(SDL_INIT_EVERYTHING);
51 
52  window = SDL_CreateWindow(
53  "Fer is an ejit",
54  SDL_WINDOWPOS_UNDEFINED,
55  SDL_WINDOWPOS_UNDEFINED,
56  640, 576,
57  SDL_WINDOW_INPUT_FOCUS
58  );
59 
60  surface = SDL_GetWindowSurface(window);
61  */
62 
63  printf("SDL init\n");
64 
65  drawRect(0, 0, getScreenWidth(), getScreenHeight(), 0x00000000);
66  punch();
67 
68  return 0;
69 }
70 
71 int sdl_update(void)
72 {
73  size_t i;
74 
75  int chr = getCharRaw() % 128;
76 
77  for(i = 0; i < sizeof (keys) / sizeof (struct keymap); i++) {
78  if(keys[i].code != chr) {
79  if(keys[i].key)
80  *(keys[i].key) = 0;
81  continue;
82  }
83 
84  if(keys[i].f && keys[i].prev == 0) {
85  *(keys[i].key) = 1;
86  keys[i].f();
87  }
88 
89  keys[i].prev = *(keys[i].key);
90  *(keys[i].key) = keys[i].code == chr;
91  }
92 
93  if(button_quit) {
94  printf("frames: %d\n", frames);
95  return 1;
96  }
97 
98  return 0;
99 }
100 
101 unsigned int sdl_get_buttons(void)
102 {
103  return (button_start << 3) | (button_select << 2) | (button_b << 1) | button_a;
104 }
105 
106 unsigned int sdl_get_directions(void)
107 {
108  return (button_down << 3) | (button_up << 2) | (button_left << 1) | button_right;
109 }
110 
111 unsigned int *sdl_get_framebuffer(void)
112 {
113  return gb_framebuffer;
114 }
115 
116 void gb_display_helper(uint8_t* display_addr) {
117  size_t real_bpp = framebuffer_bpp >> 3;
118 
119  for(register int i = 0; i < HEIGHT; i++) {
120  int iy = i + offsety;
121 
122  for(register int j = 0; j < WIDTH; j++) {
123  uint8_t* pixels = display_addr + ((j + offsetx) * real_bpp) + iy * framebuffer_pitch;
124  uint32_t color = *(uint32_t*)(((char*)gb_framebuffer) + PIXIDX(WIDTH * 4, j * 4, i));
125 
126  pixels[0] = color & 255;
127  pixels[1] = (color >> 8) & 255;
128  pixels[2] = (color >> 16) & 255;
129  }
130  }
131 }
132 
133 void sdl_frame(void)
134 {
135  frames++;
136 
137  uint8_t* displ = (uint8_t*)getDisplayAddr();
138 
139  gb_display_helper(displ);
140 
141  // printf("Update\n");
142 }
143 
144 void sdl_quit()
145 {
146  free(gb_framebuffer);
147 }
Основные определения ядра
void drawRect(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint32_t color)
Рисуем залитый прямоугольник
Definition: pixel.c:25
Definition: sdl.c:14