-
Notifications
You must be signed in to change notification settings - Fork 0
/
media-sdl.c
178 lines (141 loc) · 3.61 KB
/
media-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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <assert.h>
#include <string.h>
#include <SDL2/SDL.h>
#include "media.h"
#define PIXEL_RADIUS 16
#define SCREEN_WIDTH (64*PIXEL_RADIUS)
#define SCREEN_HEIGHT (32*PIXEL_RADIUS)
#define WINDOW_NAME "CHIP-8 emulator"
#define NUM_PIXELS (SCREEN_WIDTH*SCREEN_HEIGHT)
#define SCREEN_RECT ((SDL_Rect){.x=0,.y=0,.w=SCREEN_WIDTH,.h=SCREEN_HEIGHT})
#define SOUND_DEV_FREQ 48000
#define SOUND_SAMPLES 1024
#define BUZZER_FREQ 440
#define BUZZER_VOL .05
unsigned char *pixels;
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *texture;
/* sound state*/
int buzzer_state = 0;
/*
┌───┬───┬───┬───┐
│ 1 │ 2 │ 3 │ C │
├───┼───┼───┼───┤
│ 4 │ 5 │ 6 │ D │
├───┼───┼───┼───┤
│ 7 │ 8 │ 9 │ E │
├───┼───┼───┼───┤
│ A │ 0 │ B │ F │
└───┴───┴───┴───┘
*/
SDL_Keycode keys[16] = {
SDLK_v,
SDLK_3, SDLK_4, SDLK_5,
SDLK_e, SDLK_r, SDLK_t,
SDLK_d, SDLK_f, SDLK_g,
SDLK_c, SDLK_b,
SDLK_6, SDLK_y, SDLK_h, SDLK_n
};
static void buzzer_callback(void* userdata, Uint8* stream, int len){
static long sample_num = 0;
(void) userdata;
for(int i=0 ; i<len ; i++){
if(buzzer_state == 0)
stream[i] = 0;
else /* saw waves at the moment */
stream[i] =
((sample_num++*256*BUZZER_FREQ/SOUND_DEV_FREQ)
%256)*BUZZER_VOL;
}
}
int m_init(int argc, char **argv){
(void)argc, (void)argv;
/*TODO: check every init */
pixels = calloc(1,NUM_PIXELS);
SDL_Init( SDL_INIT_EVERYTHING );
window = SDL_CreateWindow(
WINDOW_NAME,
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH, SCREEN_HEIGHT,
0);
renderer = SDL_CreateRenderer(
window, -1,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
texture = SDL_CreateTexture(
renderer,
SDL_PIXELFORMAT_RGB332, SDL_TEXTUREACCESS_STREAMING,
SCREEN_WIDTH, SCREEN_HEIGHT);
SDL_RenderClear(renderer);
SDL_OpenAudio(&(SDL_AudioSpec){
.freq = SOUND_DEV_FREQ,
.format = AUDIO_U8,
.channels = 1,
.samples = SOUND_SAMPLES,
.callback = buzzer_callback,
}, NULL);
SDL_PauseAudio(0);
return 0;
}
void m_quit(void){
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_CloseAudio();
SDL_Quit();
free(pixels);
}
unsigned short get_input(unsigned short input){
SDL_Event e;
while(SDL_PollEvent(&e)){
if (e.type == SDL_QUIT) {
return -1;
}
if (e.type == SDL_KEYDOWN || e.type == SDL_KEYUP) {
for(int i=0;i<16;i++){
if(e.key.keysym.sym == keys[i]){
if(e.type == SDL_KEYDOWN){
input |= 1<<i;
} else {
input &= ~(1<<i);
}
break;
}
}
}
}
return input;
}
unsigned short wait_input(unsigned short input){
/* TODO: better version */
int new_input;
while((new_input = get_input(input)) == input)
SDL_Delay(1);
return new_input;
}
void clear_screen(void){
memset(pixels,0,NUM_PIXELS);
}
void draw(int x, int y, int value){
assert(0<=x && x<64);
assert(0<=y && y<32);
for(int i=0 ; i<PIXEL_RADIUS ; i++){
for(int j=0 ; j<PIXEL_RADIUS ; j++){
pixels[(y*PIXEL_RADIUS+i)*SCREEN_WIDTH +
x*PIXEL_RADIUS+j] = value ? 255 : 0;
}
}
}
void frame(void){
void *texture_pixels;
int pitch; /* we don't actually use this, yolo */
SDL_LockTexture(texture, &SCREEN_RECT, &texture_pixels, &pitch);
memcpy(texture_pixels, pixels, NUM_PIXELS);
SDL_UnlockTexture(texture);
SDL_RenderCopy(renderer, texture, &SCREEN_RECT, &SCREEN_RECT);
SDL_RenderPresent(renderer);
SDL_RenderClear(renderer);
}
void set_buzzer_state(int state){
buzzer_state = state;
}