-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice.h
215 lines (179 loc) · 4.96 KB
/
device.h
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//TBD interface for device system generates code
#ifndef DEVICE_H
#define DEVICE_H
//#define BVM_IMPLEMENTATION
//#define SDL_DEVICE
#include "bvm_type.h"
#ifdef SDL_DEVICE
#include<SDL2/SDL.h>
typedef enum {
INIT_SDL,
CHECK_EVENT,
PUSH_EVENT_FROM_QUEUE,
RENDERER_CLEAR,
SET_RENDER_DRAW_COLOR,
SDL_RENDER_FILL_RECT,
SDL_RENDER_PRESENT,
SDL_DELAY,
SDL_MOUSE,
NUM_OF_DEVICES
}DeviceType;
static const char* device_name[] = {
"initSdl",
"checkEvent",
"pushEvent",
"renderClear",
"drawColor",
"renderRect",
"renderPresent",
"sdlDelay",
"sdlMouse",
"NUM_OF_DEVICES",
};
typedef struct device{
void (*func_pointer)(Stack*);
int stackSize;
}Devices;
//ALL STUF ARE PASED AS GLOBAL VARS FOR EXAMPLE WINDOW, RENDERER itd...
//NEKA SA SAD BUDE GLOBALNO
static volatile Devices devices[NUM_OF_DEVICES];
//MAKE THIS STUF BE PASED SOMEHOW
//PROBLY PASS A STACK
static SDL_Window *window;
static SDL_Renderer *renderer;
#define WIDTH 800
#define HEIGHT 600
//(WIDTH HEIGHT --)
static inline void initSdl(Stack *stack){
//Stack *s;
printf("Inited sdl!!!\n");
Word width = stackPop(stack);
Word height = stackPop(stack);
printf("Width: %d Height %d\n", width._asI64, height._asI64);
SDL_CreateWindowAndRenderer(width._asI64, height._asI64, NULL, &window, &renderer);
//while(1){
//}
return;
}
//(-- key event.type)
static inline void pushEventFromQueue(Stack *stack){
//Word typeOfEvent = stackPop(stack);
SDL_Event event;
if(SDL_PollEvent(&event)){
stackPush(stack, (i64)event.key.keysym.sym);
stackPush(stack, (i64)event.type);
}
else{
stackPush(stack, -1);
stackPush(stack, -1); //NO EVENT IN A QUEUE
}
}
//(err +)
static inline void renderClear(Stack *stack){
int err = SDL_RenderClear(renderer);//MAYBE PUSHES ERROR CODE
stackPush(stack, (i64)err);
}
//(Event +)
static inline void checkEventInLoop(Stack *stack){
Word typeOfEvent = stackPop(stack);
SDL_Event event;
//printf("Event loop\n");
if(SDL_PollEvent(&event)){
if(event.type == typeOfEvent._asI64){
//MAYBE SEND DATA TO A STACK
exit(-1);
}
}
}
//(a, g, b, r ----)
static inline void drawColor(Stack *stack){
Word a = stackPop(stack);
Word b = stackPop(stack);
Word g = stackPop(stack);
Word r = stackPop(stack);
SDL_SetRenderDrawColor(renderer, (u8)r._asI64, (u8)g._asI64, (u8)b._asI64, (u8)a._asI64);
}
//(h,w,y,x ----)
static inline void rednerRect(Stack *stack){
Word h = stackPop(stack);
Word w = stackPop(stack);
Word y = stackPop(stack);
Word x = stackPop(stack);
SDL_Rect r;
r.h = (int)h._asI64;
r.w = (int)w._asI64;
r.y = (int)y._asI64;
r.x = (int)x._asI64;
if(SDL_RenderFillRect(renderer, &r) != 0){
printf("Error in drawing of a rectangle\n");
}
//SDL_Delay(1);
}
//(--)
static inline void renderPresent(Stack *stack){
//printf("Drawing\n");
SDL_RenderPresent(renderer);
//printf("Drawing\n");
}
//DELAY(dealy --)
static inline void sdlDelay(Stack *stack){
Word a = stackPop(stack);
SDL_Delay((u32)a._asI64);
}
//MOUSE(-- x, y)
static inline void sdlMouse(Stack *stack){
int X, Y;
SDL_GetMouseState(&X, &Y);
stackPush(stack, (i64)X);
stackPush(stack, (i64)Y);
}
static inline void initDevices(Stack *stack){
printf("Devices init\n");
//INIT SDL
devices[INIT_SDL].func_pointer = &initSdl;
devices[INIT_SDL].stackSize = -2;
//CHECK EVENT
devices[CHECK_EVENT].func_pointer = &checkEventInLoop;
devices[CHECK_EVENT].stackSize = -1;
//PUSH EVENT FROM QUUES
devices[PUSH_EVENT_FROM_QUEUE].func_pointer = &pushEventFromQueue;
devices[PUSH_EVENT_FROM_QUEUE].stackSize = +2;
//CLEAR RENDERER
devices[RENDERER_CLEAR].func_pointer = &renderClear;
devices[RENDERER_CLEAR].stackSize = +1;
//SET DRAW COLOR
devices[SET_RENDER_DRAW_COLOR].func_pointer = &drawColor;
devices[SET_RENDER_DRAW_COLOR].stackSize = -4;
//DRAW RECR
devices[SDL_RENDER_FILL_RECT].func_pointer = &rednerRect;
devices[SDL_RENDER_FILL_RECT].stackSize = -4;
//RENDER *renderer
devices[SDL_RENDER_PRESENT].func_pointer = &renderPresent;
devices[SDL_RENDER_PRESENT].stackSize = 0;
//DELAY Sdl_Delay
devices[SDL_DELAY].func_pointer = &sdlDelay;
devices[SDL_DELAY].stackSize = -1;
//MOUSE Sdl_Delay
devices[SDL_MOUSE].func_pointer = &sdlMouse;
devices[SDL_MOUSE].stackSize = +2;
}
#endif
#ifndef SDL_DEVICE
typedef enum {
NUM_OF_DEVICES
}DeviceType;
static const char* device_name[] = {
"NUM_OF_DEVICES",
};
typedef struct device{
void (*func_pointer)(int);
int stackSize;
}Devices;
//ALL STUF ARE PASED AS GLOBAL VARS FOR EXAMPLE WINDOW, RENDERER itd...
//NEKA SA SAD BUDE GLOBALNO
static Devices devices[NUM_OF_DEVICES];
static inline void initDevices(Stack *stack){
printf("Devices init non\n");
}
#endif
#endif