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
|
#include<stdio.h>
#include<time.h>
#include "gfx.h"
#include "main.h"
#include "gui.h"
int loop;
float sum;
butt butts;
tbuff texts;
std::string output;
int main (int argc, char * argv[]) {
//inits program
initgfx(400,600, "SDL calculator");
//main loop, this is defined in main.h and global
loop = 1;
SDL_Texture * ttp = loadtexture("img/blue.bmp");
sum = -1;
//adds the gui elements
butts.add(50,400,50,20, "reset");
butts.add(250,400,100,20, "calculate");
butts.add(380,0,20,20, "x");
texts.add(50,50,300,20);
texts.add(50,100,300,20);
texts.add(50,150,300,20);
texts.add(50,200,300,20);
while(loop) {
//polls events, which are in a buffer
//returns 1 if there is event in buffer
//returns 0 if buffer is empty
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
//loop is a global defined in main.h
case SDL_QUIT:
printf("loop = 0;\n");
loop = 0;
break;
//adds to the text input manager
case SDL_TEXTINPUT:
//if the input buffer was empty before,
//make it the text, which is char[] which
//is *char
texts.charin( event.text.text);
printf("text input event\n");
break;
//this only applies if you're typing in chinese
case SDL_TEXTEDITING:
printf("text editing event\n");
break;
//for when the moose moves
case SDL_MOUSEMOTION:
butts.hover(event.motion.x, event.motion.y);
texts.hover(event.motion.x, event.motion.y);
break;
//left click only, double clicks cancer out so only odd
case SDL_MOUSEBUTTONDOWN:
if(( event.button.button == SDL_BUTTON_LEFT) && ((event.button.clicks % 2) != 0)) {
butts.press(event.button.x, event.button.y);
texts.press(event.button.x, event.button.y);
}
break;
}
}
//checks the states of the buttons and handles them accordingly
if (butts.state(0) & TOGGLE) {
//returns all the text buffers to ""
texts.reset();
//toggles the state of the button back to off
butts.unpress(0);
}
//actually calculates
if (butts.state(1) & TOGGLE) {
sum = 0;
//since only error is an exception
try {
sum += stof(texts.buffs[0]);
sum += stof(texts.buffs[1]);
sum += stof(texts.buffs[2]);
sum += stof(texts.buffs[3]);
sum = sum / 4.0;
output = std::to_string(sum);
//I know that sum can be -1, but NULL doesn't work well in cpp
//and I don't want to use a second variable
} catch (std::invalid_argument& a) {
sum = -1;
//untoggles buton
output = "inputs not valid";
}
butts.unpress(1);
}
//quits the program
if (butts.state(2) & TOGGLE) {
loop = false;
butts.unpress(2);
}
cleargfx();
//grade score color
SDL_Color outc;
outc.r = 255;
outc.g = 255;
outc.b = 255;
outc.a = 255;
//the text box labesl
drawtext(0,50,"Test1",outc);
drawtext(0,100,"Test2",outc);
drawtext(0,150,"Test3",outc);
drawtext(0,200,"Test4",outc);
//load grade image
SDL_Surface * tsp;
SDL_Texture * ttp;
//so we don't assign a grade to NOT VALID
if (sum != -1) {
if (sum >= 90) {
tsp = SDL_LoadBMP("img/A.bmp");
drawtext(100,450,"A",outc);
} else if( sum >= 80) {
tsp = SDL_LoadBMP("img/B.bmp");
drawtext(100,450,"B",outc);
} else if (sum >= 70){
tsp = SDL_LoadBMP("img/C.bmp");
drawtext(100,450,"C",outc);
} else if (sum >= 60){
tsp = SDL_LoadBMP("img/D.bmp");
drawtext(100,450,"D",outc);
} else {
tsp = SDL_LoadBMP("img/F.bmp");
drawtext(100,450,"F",outc);
}
ttp = SDL_CreateTextureFromSurface(renderp, tsp);
drawimg(100,450,50,50,ttp);
SDL_FreeSurface(tsp);
SDL_DestroyTexture(ttp);
}
//draws the grade score
drawtext(180,450,output.c_str(),outc);
//draws all buttons
butts.drawbutt();
//draws text buffer
texts.drawtbuff();
//updates frame
//since only error is an exception
//all the work is done here
drawimg(0,0,20,20,"img/red.bmp");
drawimg(20,20,10,10,ttp);
drawrect(0,20,10,10,255,255,255,50);
updategfx();
}
SDL_DestroyTexture(ttp);
quitgfx();
return 0;
}
|