-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell_sdl.c
126 lines (112 loc) · 2.54 KB
/
shell_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
#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL/SDL_thread.h>
#include <SDL/SDL_mutex.h>
#include "SDL_draw.h"
#include <setjmp.h>
#include "wsfn.h"
/* SDL backend */
#define WIDTH 500
#define HEIGHT 500
#define ORIG_X (WIDTH / 2)
#define ORIG_Y (HEIGHT / 2)
#define SCALE 10
#define XX(x) (ORIG_X + (x) * SCALE)
#define YY(y) (ORIG_Y - (y) * SCALE)
#define INTERVAL 3
int done;
SDL_Surface *screen;
jmp_buf env;
void delay(void)
{
static Uint32 next;
Uint32 now;
while((now = SDL_GetTicks() ) <= next)
{
if(done) longjmp(env, 1);
SDL_Delay(1);
}
next = now + INTERVAL;
}
static void init(struct WSFN *vm)
{
/* draw background */
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 25, 45, 60) );
/* draw axis */
Draw_Line(screen, 0, ORIG_Y, WIDTH - 1, ORIG_Y,
SDL_MapRGB(screen->format, 0x20, 0x50, 0x40) );
Draw_Line(screen, ORIG_X, 0, ORIG_X, HEIGHT - 1,
SDL_MapRGB(screen->format, 0x20, 0x50, 0x40) );
SDL_Flip(screen);
}
static void move(struct WSFN *vm)
{
Uint32 _x1 = XX(vm->_x), _y1 = YY(vm->_y), x1 = XX(vm->x), y1 = YY(vm->y);
if(_x1 < 0 || _y1 < 0 || x1 < 0 || y1 < 0 ||
_x1 >= WIDTH || _y1 >= HEIGHT || x1 >= WIDTH || y1 >= HEIGHT) return;
Draw_Line(screen, _x1, _y1, x1, y1, SDL_MapRGB(screen->format, 0xfa, 0x3f, 0xa0));
SDL_Flip(screen);
delay();
}
static char *input(const char *prompt, char *buf, int size, FILE *in)
{
char *p;
printf(prompt);
p = fgets(buf, size, in);
if(!p || feof(in)) return NULL;
if( (p = strchr(buf, '\n')) ) *p = '\0';
return buf;
}
int console_func(void *arg)
{
char buf[128];
while(!done)
{
if(!input(">> ", buf, sizeof(buf), stdin) )
{
done = 1;
break;
}
if(!setjmp(env))
eval((struct WSFN *)arg, buf);
SDL_Delay(1);
}
return 0;
}
SDL_Surface * InitCanvas(void)
{
if(0 != SDL_Init(SDL_INIT_VIDEO) ||
NULL == (screen = SDL_SetVideoMode(WIDTH, HEIGHT, 0, SDL_HWSURFACE | SDL_DOUBLEBUF)))
return NULL;
SDL_WM_SetCaption("WSFN (SDL)", 0);
atexit(SDL_Quit);
init(NULL);
return screen;
}
int main(int argc, char *argv[])
{
SDL_Thread *console;
SDL_Event event;
struct WSFN wsfn = {0};
setup(&wsfn, init, move);
if(NULL == InitCanvas() ) return 1;
console = SDL_CreateThread(console_func, (void *)&wsfn);
while (!done)
{
if (SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT ||
(event.type == SDL_KEYDOWN &&
(event.key.keysym.sym == SDLK_ESCAPE ||
event.key.keysym.sym == SDLK_q)) )
{
SDL_KillThread(console);
console = NULL;
done = 1;
}
}
SDL_Delay(1);
}
if(console) SDL_WaitThread(console, NULL);
return 0;
}