From 8eb0808fecb8c57eff877fddf38db91b1f067921 Mon Sep 17 00:00:00 2001 From: knolax <1339802534.kk@gmail.com> Date: Tue, 28 Feb 2017 11:07:10 -0500 Subject: initial commit, this is the gfx library which serves as a crappy wrapper for SDL. --- example/Terminus.ttf | Bin 0 -> 423992 bytes example/img/A.bmp | Bin 0 -> 120122 bytes example/img/B.bmp | Bin 0 -> 120122 bytes example/img/C.bmp | Bin 0 -> 120122 bytes example/img/D.bmp | Bin 0 -> 120122 bytes example/img/F.bmp | Bin 0 -> 120122 bytes example/img/back.bmp | Bin 0 -> 768122 bytes example/img/black.bmp | Bin 0 -> 120122 bytes example/img/blue.bmp | Bin 0 -> 120122 bytes example/img/butt.bmp | Bin 0 -> 768122 bytes example/img/orange.bmp | Bin 0 -> 120122 bytes example/img/red.bmp | Bin 0 -> 120122 bytes example/img/white.bmp | Bin 0 -> 120122 bytes example/main | Bin 0 -> 70656 bytes example/main.cpp | 157 +++++++++++++++++++++++++++++++++++++++++++++++++ example/main.h | 4 ++ example/main.o | Bin 0 -> 48424 bytes example/makefile | 6 ++ 18 files changed, 167 insertions(+) create mode 100644 example/Terminus.ttf create mode 100644 example/img/A.bmp create mode 100644 example/img/B.bmp create mode 100644 example/img/C.bmp create mode 100644 example/img/D.bmp create mode 100644 example/img/F.bmp create mode 100644 example/img/back.bmp create mode 100644 example/img/black.bmp create mode 100644 example/img/blue.bmp create mode 100644 example/img/butt.bmp create mode 100644 example/img/orange.bmp create mode 100644 example/img/red.bmp create mode 100644 example/img/white.bmp create mode 100755 example/main create mode 100644 example/main.cpp create mode 100644 example/main.h create mode 100644 example/main.o create mode 100644 example/makefile (limited to 'example') diff --git a/example/Terminus.ttf b/example/Terminus.ttf new file mode 100644 index 0000000..3eb75d2 Binary files /dev/null and b/example/Terminus.ttf differ diff --git a/example/img/A.bmp b/example/img/A.bmp new file mode 100644 index 0000000..b6e2f86 Binary files /dev/null and b/example/img/A.bmp differ diff --git a/example/img/B.bmp b/example/img/B.bmp new file mode 100644 index 0000000..852cfde Binary files /dev/null and b/example/img/B.bmp differ diff --git a/example/img/C.bmp b/example/img/C.bmp new file mode 100644 index 0000000..a8b98a6 Binary files /dev/null and b/example/img/C.bmp differ diff --git a/example/img/D.bmp b/example/img/D.bmp new file mode 100644 index 0000000..d8f5b7d Binary files /dev/null and b/example/img/D.bmp differ diff --git a/example/img/F.bmp b/example/img/F.bmp new file mode 100644 index 0000000..b13dce9 Binary files /dev/null and b/example/img/F.bmp differ diff --git a/example/img/back.bmp b/example/img/back.bmp new file mode 100644 index 0000000..97992f5 Binary files /dev/null and b/example/img/back.bmp differ diff --git a/example/img/black.bmp b/example/img/black.bmp new file mode 100644 index 0000000..c36403e Binary files /dev/null and b/example/img/black.bmp differ diff --git a/example/img/blue.bmp b/example/img/blue.bmp new file mode 100644 index 0000000..c36403e Binary files /dev/null and b/example/img/blue.bmp differ diff --git a/example/img/butt.bmp b/example/img/butt.bmp new file mode 100644 index 0000000..7c732ed Binary files /dev/null and b/example/img/butt.bmp differ diff --git a/example/img/orange.bmp b/example/img/orange.bmp new file mode 100644 index 0000000..00f66a2 Binary files /dev/null and b/example/img/orange.bmp differ diff --git a/example/img/red.bmp b/example/img/red.bmp new file mode 100644 index 0000000..d1588c5 Binary files /dev/null and b/example/img/red.bmp differ diff --git a/example/img/white.bmp b/example/img/white.bmp new file mode 100644 index 0000000..f3dca25 Binary files /dev/null and b/example/img/white.bmp differ diff --git a/example/main b/example/main new file mode 100755 index 0000000..02712dc Binary files /dev/null and b/example/main differ 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 +#include +#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; +} + diff --git a/example/main.h b/example/main.h new file mode 100644 index 0000000..e02ea4b --- /dev/null +++ b/example/main.h @@ -0,0 +1,4 @@ +#ifndef MAIN_H +#define MAIN_H +extern int loop; +#endif diff --git a/example/main.o b/example/main.o new file mode 100644 index 0000000..ca599d7 Binary files /dev/null and b/example/main.o differ diff --git a/example/makefile b/example/makefile new file mode 100644 index 0000000..9643478 --- /dev/null +++ b/example/makefile @@ -0,0 +1,6 @@ +main : main.o + g++ main.o -o main -L/home/knolax/code/cpp/gfx -lgui -lgfx -lSDL2 -lSDL2_ttf +main.o : main.cpp + g++ -c main.cpp -I/home/knolax/code/cpp/gfx +clean : + rm gfx.o main.o gui.o -- cgit v1.1