-
Notifications
You must be signed in to change notification settings - Fork 0
/
screen_sdl.c
147 lines (123 loc) · 3.36 KB
/
screen_sdl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <SDL2/SDL.h>
#include <stdio.h>
#include <stdarg.h>
#include "main.h"
#include "timing.h"
#include "vm.h"
#include "screen_sdl.h"
#define SDL_SCREEN_WIDTH 1024
#define SDL_SCREEN_HEIGHT 512
#define SDL_PIXEL_WIDTH 16
#define SDL_PIXEL_HEIGHT 16
SDL_Window *window;
SDL_Surface *surface;
SDL_Renderer *renderer;
// TODO: return an error code and handle errors in main.c, maybe?
// TODO: pass arguments such as screen width, screen height
void sdl_init()
{
if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
fprintf(stderr, "error: could not init SDL2: %s\n", SDL_GetError());
exit(-1);
}
window = SDL_CreateWindow
(
"chp8",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SDL_SCREEN_WIDTH, SDL_SCREEN_HEIGHT,
SDL_WINDOW_SHOWN
);
if(!window)
{
fprintf(stderr, "error: could not create window: %s\n", SDL_GetError());
exit(-1);
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if(!renderer)
{
fprintf(stderr, "error: could not create renderer: %s\n", SDL_GetError());
exit(-1);
}
surface = SDL_GetWindowSurface(window);
SDL_StartTextInput();
}
void sdl_end()
{
SDL_StopTextInput();
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
void sdl_draw(vm_t *vm)
{
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_Rect rect;
if(vm->graphicsmode == LORES)
{
for(int i = 0; i < SCREEN_SIZE; i++)
{
if(!itocoord(i, &rect.x, &rect.y, SCREEN_WIDTH, SCREEN_SIZE))
continue;
rect.x *= SDL_PIXEL_WIDTH;
rect.y *= SDL_PIXEL_HEIGHT;
rect.w = SDL_PIXEL_WIDTH;
rect.h = SDL_PIXEL_HEIGHT;
if(vm->screen[i])
SDL_RenderFillRect(renderer, &rect);
}
}
else
{
for(int i = 0; i < SCREEN_SIZE_HIRES; i++)
{
if(!itocoord(i, &rect.x, &rect.y, SCREEN_WIDTH_HIRES, SCREEN_SIZE_HIRES))
continue;
rect.x *= SDL_PIXEL_WIDTH / 2;
rect.y *= SDL_PIXEL_HEIGHT / 2;
rect.w = SDL_PIXEL_WIDTH / 2;
rect.h = SDL_PIXEL_HEIGHT / 2;
if(vm->screenhr[i])
SDL_RenderFillRect(renderer, &rect);
}
}
SDL_RenderPresent(renderer);
}
// TODO: sdl_drawtext
void sdl_drawtext(int y, int x, const char *format, ...)
{
//char buf[2048];
//va_list vargs;
//va_start(vargs, format);
//vsnprintf(buf, sizeof(buf), format, vargs);
//va_end(vargs);
}
int pollevent_bf = NOINP_KEYCODE;
timing_t pollevent_buf_cl = 0;
int sdl_input()
{
const timing_t pollevent_buf_cl_target = hztotiming(GETCH_HZ);
timing_t cl = now();
if(cl - pollevent_buf_cl >= pollevent_buf_cl_target)
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
pollevent_bf = 'h';
break;
case SDL_TEXTINPUT:
pollevent_bf = event.text.text[0];
break;
default:
pollevent_bf = NOINP_KEYCODE;
break;
}
}
}
return pollevent_bf;
}