-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain3.c
More file actions
183 lines (166 loc) · 5.26 KB
/
Copy pathmain3.c
File metadata and controls
183 lines (166 loc) · 5.26 KB
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
/*
NEW IN THE LIBRARY:
- user-written event loop instead of calling the library event loop
this should give the user more control and allow him to structure the program better
- keyboard and mouse callbacks
- timer to measure elapsed time
TEST PROGRAM IDEA:
- Update()
* change the color of the rectangle every second
- Render()
* clears the screen and draws the rectangle every frame before displaying it
- MouseCallback()
* when a button is clicked, drag the rect to the position of the mouse
* when a button is released, reset rect to default position
- KeyboardCallback()
* move rect UP/DOWN/LEFT/RIGHT
- ButtonCallback()
* resize rect
NOTES:
- widgets should be unaffected by the deletion of the screen by default (TODO: maybe
let the user delete/setvisible if he wants, something like wwWidget_DestroyOnClear(wwTrue),
or each widget should know if it should be destroyed/visible or not)
*/
#include "ww.h"
#include <stdbool.h>
extern FILE *fp; // used for debugging purposes - learn how to printf() to console from WinAPI :D
FILE *fp1;
wwColor color; // could put this in a struct with the rect
unsigned short buttonId; // could pass this as parameter as well
int counter = 0; // number of frames passed since starting the timer
const int FPS = 30;
const int frameDelay = 1000 / FPS;
void Update(wwWindowAndRenderer *win, void *userData)
{
if(counter % FPS == 0)
{
color.red = rand() % 256;
color.green = rand() % 256;
color.blue = rand() % 256;
color.alpha = 255;
}
}
void Render(wwWindowAndRenderer *win, void *userData)
{
wwRenderer_Clear(win);
wwRenderer_DrawRectangle(win, (wwRect*) userData, color);
wwRenderer_Display(win);
}
void MouseCallback(wwWindowAndRenderer *win, int x, int y, wwMouseEvent event, void *userData)
{
wwRect *rect = (wwRect*) userData;
switch(event)
{
case WWMOUSE_LBUTTON_PRESSED:
case WWMOUSE_RBUTTON_PRESSED:
rect->x = x;
rect->y = y;
break;
case WWMOUSE_LBUTTON_RELEASED:
case WWMOUSE_RBUTTON_RELEASED:
rect->x = 200;
rect->y = 100;
break;
default:
printf("Unknown mouse event\n");
break;
}
}
void KeyboardCallback(wwWindowAndRenderer *win, wwKey key, wwKeyboardEvent event, void *userData)
{
wwRect *rect = (wwRect*) userData;
switch(event){
case WWKEYBOARD_KEYPRESSED:{
switch(key){
case WWKEY_DOWNARROW:
rect->y += 20;
break;
case WWKEY_UPARROW:
rect->y -= 20;
break;
case WWKEY_LEFTARROW:
rect->x -= 20;
break;
case WWKEY_RIGHTARROW:
rect->x += 20;
break;
default:
printf("Unknown key pressed %lc\n", (wchar_t) key);
break;
}
break;
}
case WWKEYBOARD_KEYRELEASED:{
switch(key){
case WWKEY_W:
case WWKEY_w:
printf("ww released\n");
rect->y -= 20;
break;
case WWKEY_S:
case WWKEY_s:
rect->y += 20;
break;
case WWKEY_D:
case WWKEY_d:
rect->x += 20;
break;
case WWKEY_A:
case WWKEY_a:
rect->x -= 20;
break;
default:
printf("Unknown key released %x\n", key);
break;
}
break;
}
default:
printf("Unknown key event\n");
break;
}
}
void buttonCallback(wwButton *button, wwWindowAndRenderer *win)
{
wwRect *rect = (wwRect*) button->widget.userData;
wwButton_SetText(button, L"It works");
rect->width += 20;
rect->height -= 20;
}
// TODO: create an init function for the library to abstract WinMain(params) and use the default main() entry point
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
wwWindowAndRenderer *win = wwWindowAndRenderer_Create(hInstance, L"Buttons Window", 100, 100, 800, 600);
wwButton *button = wwButton_Create(win, L"MyButton", 100, 100, 100, 50);
wwRect rect = {.x = 100, .y = 200, .width = 200, .height = 400};
ww_SetMouseCallback(win, MouseCallback, &rect);
ww_SetKeyboardCallback(win, KeyboardCallback, &rect);
buttonId = wwButton_GetId(button);
wwButton_SetCallback(button, buttonCallback, &rect);
// call the init function after setting up stuff but before executing the main loop
wwWindowAndRenderer_Init(win);
wwTimer timer;
wwTimer_Init(&timer);
while(ww_Running(win) == wwTrue)
{
wwTimer_Start(&timer);
Render(win, &rect);
ww_ProcessEvents(win);
Update(win, NULL);
int frameTime = wwTimer_End(&timer); // maybe wwTimer_Stop?
if(frameTime < frameDelay)
wwTimer_Delay(frameDelay - frameTime);
counter++;
}
wwWindowAndRenderer_Destroy(win);
return 0;
}
// left mouse click - move left
// left mouse release - change shape
// right mouse click - move right
// right mouse release - change shape
// scroll up - move up
// scroll down - move down
// scroll click - change color
// scroll release - change back color
// try keys 0, -, +, A, a, SPACE, ARROWS