-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmenu.cpp
154 lines (117 loc) · 3.09 KB
/
menu.cpp
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
#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <string>
#include "menu.h"
LButton menus[4];
LTexture menudefault;
bool menuloadMedia()
{
//Loading success flag
bool success = true;
//Load default surface
menudefault. loadFromFile( "pic/menu0.png" );
if( !menus[0].mCurrentsurface. loadFromFile( "pic/menu0.png" ) )
{
printf( "Failed to load button sprite texture!\n" );
success = false;
}
menus[0].mCurrentsurface. loadFromFile( "pic/menu1.png" );
if( !menus[0].mCurrentsurface. loadFromFile( "pic/menu1.png" ) )
{
printf( "Failed to load button sprite texture!\n" );
success = false;
}
menus[1].mCurrentsurface.loadFromFile( "pic/menu2.png" );
if(!menus[1].mCurrentsurface.loadFromFile( "pic/menu2.png" ) )
{
printf( "Failed to load default image!\n" );
success = false;
}
menus[2].mCurrentsurface.loadFromFile( "pic/menu3.png" );
if(! menus[2].mCurrentsurface.loadFromFile( "pic/menu3.png" ))
{
printf( "Failed to load default image!\n" );
success = false;
}
menus[3].mCurrentsurface.loadFromFile( "pic/menu4.png" );
if( !menus[3].mCurrentsurface.loadFromFile( "pic/menu4.png" ))
{
printf( "Failed to load default image!\n" );
success = false;
}
menus[0].setPosition(767,165);
menus[1].setPosition(784,295);
menus[2].setPosition(682,410);
menus[3].setPosition(817,537);
menus[0].BUTTON_HEIGHT = 130;
menus[1].BUTTON_HEIGHT = 115;
menus[2].BUTTON_HEIGHT = 127;
menus[3].BUTTON_HEIGHT = 117;
menus[0].BUTTON_WIDTH = 1024-767;
menus[1].BUTTON_WIDTH = 1024-784;
menus[2].BUTTON_WIDTH = 1024-682;
menus[3].BUTTON_WIDTH = 1024-817;
return success;
}
void menuclose()
{
//Deallocate surfaces
for( int i = 0; i < 4; ++i )
{
menus[ i ].mCurrentsurface.free();
}
menudefault.free();
}
int menu()
{
int turn=0;
//Start up SDL and create window
//Load media
if( !menuloadMedia() )
{
printf( "Failed to load media!\n" );
}
else
{
//Main loop flag
bool quit = false;
//Event handler
SDL_Event e;
menudefault.render(0,0);
//While application is running
while( !quit )
{
//Handle events on queue
while( SDL_PollEvent( &e ) != 0 )
{
//User requests quit
if( e.type == SDL_QUIT )
{
quit = true;
}
//Handle button events
for( int i = 0; i < 4; ++i )
{
menus[ i ].handleEvent( &e );
}
}
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderClear( gRenderer );
menudefault.render(0,0);
for( int i = 0; i <4; ++i )
{ if(menus[i].show==true)
menus[ i ].render();
}
//Update the surface
SDL_RenderPresent( gRenderer );
if(menus[0].press==true) {menus[0].press=false;turn=1; break; }
if(menus[1].press==true) {menus[1].press=false;turn=2; break; }
if(menus[2].press==true) {menus[2].press=false;turn=3; break; }
if(menus[3].press==true) {menus[3].press=false;turn=4; break; }
}
}
//Free resources and close SDL
menuclose();
return turn;
}