summaryrefslogtreecommitdiff
path: root/example/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'example/main.cpp')
-rw-r--r--example/main.cpp157
1 files changed, 157 insertions, 0 deletions
diff --git a/example/main.cpp b/example/main.cpp
new file mode 100644
index 0000000..67f4487
--- /dev/null
+++ b/example/main.cpp
@@ -0,0 +1,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;
+}
+