forked from xdsopl/robot36
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdl.c
162 lines (147 loc) · 3.58 KB
/
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
/*
robot36 - encode and decode images using SSTV in Robot 36 mode
Written in 2011 by <Ahmet Inan> <[email protected]>
To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <SDL.h>
#include "img.h"
struct data {
uint8_t *pixel;
int quit;
int stop;
int busy;
int okay;
SDL_Surface *screen;
SDL_Thread *thread;
};
struct sdl {
struct img base;
struct data *data;
};
void handle_events()
{
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_q:
exit(0);
break;
case SDLK_ESCAPE:
exit(0);
break;
default:
break;
}
break;
case SDL_QUIT:
exit(0);
break;
default:
break;
}
}
}
int update_sdl(void *ptr)
{
struct data *data = (struct data *)ptr;
while (!data->quit) {
if (data->stop) {
data->busy = 0;
SDL_Delay(100);
continue;
}
data->busy = 1;
if (data->okay) {
for (int i = 0; i < data->screen->w * data->screen->h; i++) {
uint8_t *src = data->pixel + i * 3;
uint8_t *dst = data->screen->pixels + i * 4;
dst[0] = src[2];
dst[1] = src[1];
dst[2] = src[0];
dst[3] = 0;
}
}
SDL_Flip(data->screen);
SDL_Delay(100);
handle_events();
}
return 0;
}
void close_sdl(struct img *img)
{
struct sdl *sdl = (struct sdl *)(img->data);
sdl->data->okay = 0;
sdl->data->stop = 1;
while (sdl->data->busy)
SDL_Delay(50);
sdl->data->stop = 0;
free(sdl->base.pixel);
free(sdl);
}
int open_sdl_write(struct img **p, char *name, int width, int height)
{
struct sdl *sdl = (struct sdl *)malloc(sizeof(struct sdl));
sdl->base.close = close_sdl;
sdl->base.data = (void *)sdl;
static struct data *data = 0;
if (!data) {
data = (struct data *)malloc(sizeof(struct data));
data->quit = 0;
data->stop = 1;
data->okay = 0;
data->busy = 0;
atexit(SDL_Quit);
SDL_Init(SDL_INIT_VIDEO);
data->thread = SDL_CreateThread(update_sdl, data);
if (!data->thread) {
fprintf(stderr, "Unable to create thread: %s\n", SDL_GetError());
SDL_Quit();
free(data);
free(sdl);
return 0;
}
}
data->okay = 0;
data->stop = 1;
while (data->busy)
SDL_Delay(50);
data->screen = SDL_SetVideoMode(width, height, 32, SDL_SWSURFACE);
if (!data->screen) {
fprintf(stderr, "couldnt open %s window %dx%d@32\n", name, width, height);
data->quit = 1;
SDL_WaitThread(data->thread, 0);
SDL_Quit();
free(data);
free(sdl);
return 0;
}
if (data->screen->format->BytesPerPixel != 4 || data->screen->w != width || data->screen->h != height) {
fprintf(stderr, "requested %dx%d@32 but got %s window %dx%d@32\n", width, height, name, data->screen->w, data->screen->h);
data->quit = 1;
SDL_WaitThread(data->thread, 0);
SDL_Quit();
free(data);
free(sdl);
return 0;
}
SDL_WM_SetCaption("robot36", "robot36");
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
data->pixel = malloc(width * height * 3);
memset(data->pixel, 0, width * height * 3);
memset(data->screen->pixels, 0, width * height * 4);
sdl->base.width = width;
sdl->base.height = height;
sdl->data = data;
sdl->base.pixel = data->pixel;
data->okay = 1;
data->stop = 0;
*p = &(sdl->base);
return 1;
}