Skip to content

Commit f75ac57

Browse files
committed
更新:02 - UI State - 完成UIState的测试。
1 parent 8e5319e commit f75ac57

1 file changed

Lines changed: 45 additions & 5 deletions

File tree

src/main.cpp

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ SDL_Surface *screen;
88
int screen_width = 640;
99
int screen_height = 480;
1010

11+
struct UIState
12+
{
13+
int mousex;
14+
int mousey;
15+
int mousedown;
16+
17+
int hotitem;
18+
int activeitem;
19+
}
20+
uistate = {0, 0, 0, 0, 0};
21+
1122
// Simplified interface to SDL_
1223
void drawrect(int x, int y, int w, int h, int color)
1324
{
@@ -27,8 +38,9 @@ void render()
2738
// Clear the screen.
2839
drawrect(0, 0, screen_width, screen_height, 0);
2940

30-
// Draw a rectangle.
31-
drawrect(64, 48, 64, 48, 0xff);
41+
// Test the uistate is working.
42+
drawrect(uistate.mousex - 32, uistate.mousey - 24,
43+
64, 48, 0xff << (uistate.mousedown * 8));
3244

3345
// Tell SDL to update the whole screen
3446
SDL_UpdateWindowSurface(window);
@@ -78,17 +90,45 @@ int main(int argc, char *argv[])
7890
{
7991
switch (event.type)
8092
{
93+
// Update mouse position.
94+
case SDL_MOUSEMOTION:
95+
{
96+
uistate.mousex = event.motion.x;
97+
uistate.mousey = event.motion.y;
98+
} break;
99+
100+
// Update button down state if left-clicking.
101+
case SDL_MOUSEBUTTONDOWN:
102+
{
103+
if (event.button.button == 1 )
104+
{
105+
uistate.mousedown = 1;
106+
}
107+
} break;
108+
109+
// Update button down state if left-clicking.
110+
case SDL_MOUSEBUTTONUP:
111+
{
112+
if (event.button.button == 1)
113+
{
114+
uistate.mousedown = 0;
115+
}
116+
} break;
117+
81118
case SDL_KEYDOWN:
82119
{
83120

84121
} break;
85122

86123
case SDL_KEYUP:
87124
{
88-
// If escape is pressed, return (and thus, quit)
89-
if (event.key.keysym.sym == SDLK_ESCAPE)
125+
switch (event.key.keysym.sym)
90126
{
91-
return 0;
127+
// If escape is pressed, return (and thus, quit)
128+
case SDLK_ESCAPE:
129+
{
130+
return 0;
131+
}
92132
}
93133
} break;
94134

0 commit comments

Comments
 (0)