summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorknolax <1339802534.kk@gmail.com>2017-02-28 11:07:10 -0500
committerknolax <1339802534.kk@gmail.com>2017-02-28 11:07:10 -0500
commit8eb0808fecb8c57eff877fddf38db91b1f067921 (patch)
tree1d7a5a795686e3c607ed93993c34c7a377b7c161
initial commit, this is the gfx library which serves as a crappy wrapper for SDL.
-rw-r--r--example/Terminus.ttfbin0 -> 423992 bytes
-rw-r--r--example/img/A.bmpbin0 -> 120122 bytes
-rw-r--r--example/img/B.bmpbin0 -> 120122 bytes
-rw-r--r--example/img/C.bmpbin0 -> 120122 bytes
-rw-r--r--example/img/D.bmpbin0 -> 120122 bytes
-rw-r--r--example/img/F.bmpbin0 -> 120122 bytes
-rw-r--r--example/img/back.bmpbin0 -> 768122 bytes
-rw-r--r--example/img/black.bmpbin0 -> 120122 bytes
-rw-r--r--example/img/blue.bmpbin0 -> 120122 bytes
-rw-r--r--example/img/butt.bmpbin0 -> 768122 bytes
-rw-r--r--example/img/orange.bmpbin0 -> 120122 bytes
-rw-r--r--example/img/red.bmpbin0 -> 120122 bytes
-rw-r--r--example/img/white.bmpbin0 -> 120122 bytes
-rwxr-xr-xexample/mainbin0 -> 70656 bytes
-rw-r--r--example/main.cpp157
-rw-r--r--example/main.h4
-rw-r--r--example/main.obin0 -> 48424 bytes
-rw-r--r--example/makefile6
-rw-r--r--gfx.cpp152
-rw-r--r--gfx.h27
-rw-r--r--gfx.obin0 -> 9008 bytes
-rw-r--r--gui.cpp250
-rw-r--r--gui.h72
-rw-r--r--gui.obin0 -> 92736 bytes
-rw-r--r--libgfx.abin0 -> 9470 bytes
-rw-r--r--libgui.abin0 -> 103782 bytes
-rw-r--r--makefile13
27 files changed, 681 insertions, 0 deletions
diff --git a/example/Terminus.ttf b/example/Terminus.ttf
new file mode 100644
index 0000000..3eb75d2
--- /dev/null
+++ b/example/Terminus.ttf
Binary files differ
diff --git a/example/img/A.bmp b/example/img/A.bmp
new file mode 100644
index 0000000..b6e2f86
--- /dev/null
+++ b/example/img/A.bmp
Binary files differ
diff --git a/example/img/B.bmp b/example/img/B.bmp
new file mode 100644
index 0000000..852cfde
--- /dev/null
+++ b/example/img/B.bmp
Binary files differ
diff --git a/example/img/C.bmp b/example/img/C.bmp
new file mode 100644
index 0000000..a8b98a6
--- /dev/null
+++ b/example/img/C.bmp
Binary files differ
diff --git a/example/img/D.bmp b/example/img/D.bmp
new file mode 100644
index 0000000..d8f5b7d
--- /dev/null
+++ b/example/img/D.bmp
Binary files differ
diff --git a/example/img/F.bmp b/example/img/F.bmp
new file mode 100644
index 0000000..b13dce9
--- /dev/null
+++ b/example/img/F.bmp
Binary files differ
diff --git a/example/img/back.bmp b/example/img/back.bmp
new file mode 100644
index 0000000..97992f5
--- /dev/null
+++ b/example/img/back.bmp
Binary files differ
diff --git a/example/img/black.bmp b/example/img/black.bmp
new file mode 100644
index 0000000..c36403e
--- /dev/null
+++ b/example/img/black.bmp
Binary files differ
diff --git a/example/img/blue.bmp b/example/img/blue.bmp
new file mode 100644
index 0000000..c36403e
--- /dev/null
+++ b/example/img/blue.bmp
Binary files differ
diff --git a/example/img/butt.bmp b/example/img/butt.bmp
new file mode 100644
index 0000000..7c732ed
--- /dev/null
+++ b/example/img/butt.bmp
Binary files differ
diff --git a/example/img/orange.bmp b/example/img/orange.bmp
new file mode 100644
index 0000000..00f66a2
--- /dev/null
+++ b/example/img/orange.bmp
Binary files differ
diff --git a/example/img/red.bmp b/example/img/red.bmp
new file mode 100644
index 0000000..d1588c5
--- /dev/null
+++ b/example/img/red.bmp
Binary files differ
diff --git a/example/img/white.bmp b/example/img/white.bmp
new file mode 100644
index 0000000..f3dca25
--- /dev/null
+++ b/example/img/white.bmp
Binary files differ
diff --git a/example/main b/example/main
new file mode 100755
index 0000000..02712dc
--- /dev/null
+++ b/example/main
Binary files 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<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;
+}
+
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
--- /dev/null
+++ b/example/main.o
Binary files 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
diff --git a/gfx.cpp b/gfx.cpp
new file mode 100644
index 0000000..ca597d5
--- /dev/null
+++ b/gfx.cpp
@@ -0,0 +1,152 @@
+#include "gfx.h"
+//all the global variables first set here
+SDL_Window *mwindowp; // main window were everything foes
+SDL_DisplayMode * screenp;
+SDL_Renderer * renderp;
+TTF_Font * fontp;
+int maxx;
+int maxy;
+int initgfx(int x, int y,const char * title) {
+ //initializing SDL 2
+ SDL_Init(SDL_INIT_EVERYTHING);
+ //sets global variables for ease of access later
+ //they are 100 by 200 for now because DisplayMode takes long to implement
+ //screen resizes are handled by sdl events
+ //we're not gonna hcange these for now
+ maxx = x;
+ maxy = y;
+ //makes sdl compatible with opengl(for later use)
+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
+ SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
+ //creates window
+ mwindowp = SDL_CreateWindow(title, 0, 0, maxx, maxy, SDL_WINDOW_OPENGL);
+ //renderer to write to
+ //the int selects which drivers, -1 just selects first applicatble
+ renderp = SDL_CreateRenderer(mwindowp, -1,SDL_RENDERER_ACCELERATED);
+ //load truetype font
+ int err = TTF_Init();
+ fontp = TTF_OpenFont("Terminus.ttf",18);
+ if (fontp == NULL) {
+ printf("font failed to load\n");
+ printf("ttf error %s",TTF_GetError());
+ }
+ return 0;
+}
+//two draw functions for convenience
+int drawimg(int x, int y, int w, int h,SDL_Texture * tex) {
+ SDL_Rect r;
+ r.x = x;
+ r.y = y;
+ r.w = w;
+ r.h = h;
+ //null means entire source texture
+ SDL_RenderCopy(renderp, tex, NULL, &r);
+ return 0;
+
+}
+int drawimg(SDL_Rect r, SDL_Texture * tex) {
+ SDL_RenderCopy(renderp, tex, NULL, &r);
+ return 0;
+}
+SDL_Texture * loadtexture(const char * file) {
+ SDL_Surface * tsp;
+ SDL_Texture * ttp;
+ tsp = SDL_LoadBMP(file);
+ ttp = SDL_CreateTextureFromSurface(renderp, tsp);
+ SDL_SetTextureBlendMode(ttp, SDL_BLENDMODE_ADD);
+ SDL_FreeSurface(tsp);
+ return ttp;
+}
+int drawimg(int x, int y, int w, int h, const char * file) {
+ SDL_Texture * ttp = loadtexture(file);
+ drawimg(x,y,w,h,ttp);
+ SDL_DestroyTexture(ttp);
+}
+int drawrect (int x, int y, int w, int h, const char * color) {
+ std::string str;
+ str = color;
+ SDL_Rect r;
+ r.x = x;
+ r.y = y;
+ r.w = w;
+ r.h = h;
+ if (!str.compare("red")) {
+ SDL_SetRenderDrawColor(renderp,255,0,0,255);
+ }
+ if (!str.compare("blue")) {
+ SDL_SetRenderDrawColor(renderp,0,0,255,255);
+ }
+ if (!str.compare("orange")) {
+ SDL_SetRenderDrawColor(renderp,255,180,20,255);
+ }
+ if (!str.compare("white")) {
+ SDL_SetRenderDrawColor(renderp,255,255,255,255);
+ }
+ if (!str.compare("black")) {
+ SDL_SetRenderDrawColor(renderp,0,0,0,255);
+ }
+ SDL_RenderFillRect(renderp,&r);
+ SDL_SetRenderDrawColor(renderp,0,0,0,255);
+ return 0;
+}
+int drawrect (int x, int y, int w, int h,int r, int g, int b, int a) {
+ SDL_Rect rect;
+ rect.x = x;
+ rect.y = y;
+ rect.w = w;
+ rect.h = h;
+ SDL_SetRenderDrawColor(renderp,r,g,b,a);
+ SDL_SetRenderDrawBlendMode(renderp, SDL_BLENDMODE_ADD);
+ SDL_RenderFillRect(renderp,&rect);
+ SDL_SetRenderDrawColor(renderp,0,0,0,255);
+ return 0;
+}
+//draws text
+int drawtext(int x, int y,const char * text, SDL_Color c) {
+ //surface the font renderer writes to
+ SDL_Surface * sp;
+ //color of text
+ SDL_Color co;
+ co.r = 100;
+ co.g = 100;
+ co.b = 100;
+ co.a = 100;
+ //renders text with global font
+ sp = TTF_RenderText_Solid(fontp, text, c);
+ //texture to actually draw
+ SDL_Texture * tp;
+ tp = SDL_CreateTextureFromSurface(renderp, sp);
+ //the surface has to be freed because it is only used once and the pointer
+ //to it was declared in a funcion
+ SDL_FreeSurface(sp);
+ //finding the width and height of the text
+ int w;
+ int h;
+ SDL_QueryTexture(tp, NULL, NULL, &w, &h);
+ //drawing the texture
+ drawimg(x, y, w, h, tp);
+ SDL_DestroyTexture(tp);
+}
+//this is where almost all of the grame code is
+int updategfx() {
+
+ //checks the states of the buttons and handles them accordingly
+
+ //clears renderer buffer
+
+ SDL_RenderPresent(renderp);
+ return 0;
+}
+int cleargfx() {
+ SDL_RenderClear(renderp);
+}
+//quits all the libraries and releases some memory
+int quitgfx() {
+ //release of context
+ SDL_DestroyRenderer(renderp);
+ TTF_Quit();
+ SDL_Quit();
+ return 0;
+}
diff --git a/gfx.h b/gfx.h
new file mode 100644
index 0000000..f2eb889
--- /dev/null
+++ b/gfx.h
@@ -0,0 +1,27 @@
+#ifndef GFX_H
+#define GFX_H
+#include <SDL2/SDL.h>
+#include <SDL2/SDL_opengl.h>
+#include <SDL2/SDL_ttf.h>
+#include <iostream>
+#include <string>
+//these are all global variables,
+extern SDL_Window *mwindow; // main window were everything foes
+extern SDL_DisplayMode * screen;
+extern SDL_GLContext context;
+extern SDL_Renderer * renderp;
+extern TTF_Font * fontp;
+extern int maxx;
+extern int maxy;
+int initgfx(int x, int y, const char * title);
+int cleargfx();
+int quitgfx();
+int updategfx();
+int drawimg(SDL_Rect r, SDL_Texture * tex);
+int drawimg(int x, int y, int w, int h,SDL_Texture * tex);
+int drawimg(int x, int y, int w, int h, const char * file);
+SDL_Texture * loadtexture(const char * file);
+int drawrect(int x, int y, int w, int h, const char *);
+int drawrect (int x, int y, int w, int h,int r, int g, int b, int a);
+int drawtext(int x, int y,const char * text, SDL_Color c);
+#endif
diff --git a/gfx.o b/gfx.o
new file mode 100644
index 0000000..485e58e
--- /dev/null
+++ b/gfx.o
Binary files differ
diff --git a/gui.cpp b/gui.cpp
new file mode 100644
index 0000000..30889b9
--- /dev/null
+++ b/gui.cpp
@@ -0,0 +1,250 @@
+#include "gui.h"
+hitbox::hitbox() {
+ return;
+}
+int hitbox::add(int x, int y, int w, int h) {
+ SDL_Rect rr;
+ rr.x = x;
+ rr.y = y;
+ rr.w = w;
+ rr.h = h;
+ rs.resize(rs.size()+1);
+ rs[rs.size() - 1] = rr;
+ return 0;
+}
+int hitbox::check(int x, int y) {
+ int i = 0;
+ while (i < rs.size()) {
+ if ((rs[i].x <= x) && (x <= (rs[i].x + rs[i].w))) {
+ if ((rs[i].y <= y) && (y <= (rs[i].y + rs[i].h))) {
+ return i;
+ }
+ }
+ i++;
+ }
+ return -1;
+}
+SDL_Rect hitbox::get(int index) {
+ if ((index >= 0) && (index < rs.size())) {
+ return rs[index];
+ } else {
+ //if properly managed should never happen
+ SDL_Rect r;
+ r.x = -10000;
+ return r;
+ }
+}
+butt::butt() {
+ return;
+}
+int butt::add(int x, int y, int w, int h, std::string name) {
+ boxes.add(x, y, w, h);
+ names.resize(names.size() + 1);
+ names[names.size() - 1] = name;
+ states.resize(states.size() + 1);
+ //zero means not toggled and not hovered
+ states[states.size() - 1] = 0;
+ return 0;
+}
+int butt::hover(int x, int y) {
+ int i = 0;
+ while (i < states.size()) {
+ //sets hover bit to 0 by AND'ing with the opposite
+ states[i] = states[i] & (~HOVER);
+ i++;
+ }
+ int c = boxes.check(x, y);
+ if (c != -1) {
+ states[c] = states[c] | HOVER;
+ }
+ return 0;
+}
+int butt::press(int x, int y) {
+ int c = boxes.check(x, y);
+ if (c != -1) {
+ states[c] = states[c] ^ TOGGLE;
+ }
+ return 0;
+}
+//doesn't do anything for now because just press will do the trick
+int butt::unpress(int index) {
+ states[index] = states[index] & (~TOGGLE);
+}
+int butt::drawbutt() {
+ int i = 0;
+ while (i < states.size()) {
+ SDL_Rect r = boxes.get(i);
+ SDL_Color c;
+ switch (states[i]) {
+ case TOGGLE :
+ drawrect(r.x,r.y,r.w,r.h,140,140,140,255);
+ c.r = 255;
+ c.g = 255;
+ c.b = 255;
+ c.a = 255;
+ drawtext(r.x, r.y, names[i].c_str(),c);
+ break;
+ case HOVER :
+ drawrect(r.x,r.y,r.w,r.h,140,140,140,255);
+ c.r = 255;
+ c.g = 255;
+ c.b = 255;
+ c.a = 255;
+ drawtext(r.x, r.y, names[i].c_str(),c);
+ break;
+ case TOGGLE | HOVER :
+ drawrect(r.x,r.y,r.w,r.h,140,140,140,255);
+ c.r = 255;
+ c.g = 255;
+ c.b = 255;
+ c.a = 255;
+ drawtext(r.x, r.y, names[i].c_str(),c);
+ break;
+ default :
+ drawrect(r.x,r.y,r.w,r.h,140,140,140,255);
+ c.r = 0;
+ c.g = 0;
+ c.b = 0;
+ c.a = 255;
+ drawtext(r.x, r.y, names[i].c_str(),c);
+ break;
+ }
+ i++;
+ }
+ return 0;
+}
+
+int butt::state(int index){
+ if ((index >= 0) && (index < states.size())) {
+ return states[index];
+ } else {
+ return -1;
+ }
+}
+int butt::size() {
+ return states.size();
+}
+//---=[TBUFF]=---
+//
+//
+//
+tbuff::tbuff() {
+ return;
+}
+int tbuff::add(int x, int y, int w, int h) {
+ boxes.add(x, y, w, h);
+ buffs.resize(buffs.size() + 1);
+ buffs[buffs.size() - 1] = "";
+ states.resize(states.size() + 1);
+ //zero means not toggled and not hovered
+ states[states.size() - 1] = 0;
+ return 0;
+}
+int tbuff::hover(int x, int y) {
+ int i = 0;
+ while (i < states.size()) {
+ //sets hover bit to 0 by AND'ing with the opposite
+ states[i] = states[i] & (~HOVER);
+ i++;
+ }
+ int c = boxes.check(x, y);
+ if (c != -1) {
+ states[c] = states[c] | HOVER;
+ }
+ return 0;
+}
+int tbuff::press(int x, int y) {
+ int i = 0;
+ while (i < states.size()) {
+ //sets hover bit to 0 by AND'ing with the opposite
+ states[i] = states[i] & (~TOGGLE);
+ i++;
+ }
+ int c = boxes.check(x, y);
+
+ if (c != -1) {
+ states[c] = states[c] ^ TOGGLE;
+ SDL_Rect r = boxes.get(c);
+ SDL_StartTextInput();
+ SDL_SetTextInputRect(&r);
+ } else {
+ SDL_StopTextInput();
+ }
+ return 0;
+}
+//doesn't do anything for now because just press will do the trick
+int tbuff::unpress(int index) {
+ states[index] = states[index] & (~TOGGLE);
+ SDL_StopTextInput();
+}
+int tbuff::drawtbuff() {
+ int i = 0;
+ while (i < states.size()) {
+ SDL_Rect r = boxes.get(i);
+ SDL_Color c;
+ switch (states[i]) {
+ case TOGGLE :
+ drawrect(r.x,r.y,r.w,r.h,140,140,140,255);
+ c.r = 255;
+ c.g = 255;
+ c.b = 255;
+ c.a = 255;
+ drawtext(r.x, r.y, buffs[i].c_str(),c);
+ break;
+ case HOVER :
+ drawrect(r.x,r.y,r.w,r.h,180,180,180,255);
+ c.r = 0;
+ c.g = 0;
+ c.b = 0;
+ c.a = 0;
+ drawtext(r.x, r.y, buffs[i].c_str(),c);
+ break;
+ case TOGGLE | HOVER :
+ drawrect(r.x,r.y,r.w,r.h,180,180,180,255);
+ c.r = 0;
+ c.g = 0;
+ c.b = 0;
+ c.a = 0;
+ drawtext(r.x, r.y, buffs[i].c_str(),c);
+ break;
+ default :
+ drawrect(r.x,r.y,r.w,r.h,140,140,140,255);
+ c.r = 0;
+ c.g = 0;
+ c.b = 0;
+ c.a = 255;
+ drawtext(r.x, r.y, buffs[i].c_str(),c);
+ break;
+ }
+ i++;
+ }
+ return 0;
+}
+int tbuff::reset() {
+ int i = 0;
+ while (i < buffs.size()) {
+ buffs[i] = "";
+ i++;
+ }
+ return 0;
+}
+int tbuff::charin(const char *text) {
+ int i = 0;
+ while (i < states.size()) {
+ if (states[i] & TOGGLE) {
+ buffs[i] = buffs[i] + text;
+ }
+ i++;
+ }
+ return 0;
+}
+int tbuff::state(int index){
+ if ((index >= 0) && (index < states.size())) {
+ return states[index];
+ } else {
+ return -1;
+ }
+}
+int tbuff::size() {
+ return states.size();
+}
diff --git a/gui.h b/gui.h
new file mode 100644
index 0000000..2c72e80
--- /dev/null
+++ b/gui.h
@@ -0,0 +1,72 @@
+#ifndef GUI_H
+#define GUI_H
+#include <SDL2/SDL.h>
+#include<vector>
+#include <string>
+#include "gfx.h"
+//for the states, they can be OR'd together
+#define TOGGLE 1
+#define HOVER 2
+//---=[structure of gui elements]=---
+//gfxupdate handles input events and passess to
+//butt and textin classesss
+//butt and textin have hitbox members for each of their elements that
+//they take mouse data to.
+class hitbox {
+ std::vector<SDL_Rect> rs;//vector of Rects to manage
+ public:
+ hitbox();//constructor does nothing
+ int add(int x, int y, int w, int h); // there is an add but no remove
+ //because this class is not meant to be edited at runtime. the reason
+ //we don't just pass a vector is for ease of use by parent function
+ int check(int x, int y);//checks hitbox
+ SDL_Rect get(int index);
+};
+class butt {
+ std::vector<std::string> names; // vector of strings that are the names
+ //of buttons
+ std::vector<int> states;
+ hitbox boxes;//manager of the hitboxes of all the buttons
+
+ public :
+ butt(); // empty constructor
+ int add(int x, int y, int w, int h, std::string name); // not meant
+ //to be done during runtime, it's not in the constructor for ease
+
+ //of use handle the three mouseevents from updategfx(), not return
+ //because they're not going to run into a runtime error the parent
+ //has to handle
+ int hover(int x, int y);
+ int press(int x,int y );
+ int unpress(int index);
+ //as the Renderer is a global var, draw does not take any inputs
+ int drawbutt();
+ //get's the state of a button;
+ int state(int index);
+ int size();
+};
+class tbuff {
+ public :
+ std::vector<std::string> buffs; // vector of strings that are the names
+ //of buttons
+ std::vector<int> states;
+ hitbox boxes;//manager of the hitboxes of all the buttons
+ tbuff(); // empty constructor
+ int add(int x, int y, int w, int h); // not meant
+ //to be done during runtime, it's not in the constructor for ease
+
+ //of use handle the three mouseevents from updategfx(), not return
+ //because they're not going to run into a runtime error the parent
+ //has to handle
+ int hover(int x, int y);
+ int press(int x,int y );
+ int unpress(int index);
+ //as the Renderer is a global var, draw does not take any inputs
+ int drawtbuff();
+ //get's the state of a button;
+ int state(int index);
+ int size();
+ int reset();
+ int charin(const char *);
+};
+#endif
diff --git a/gui.o b/gui.o
new file mode 100644
index 0000000..7032d20
--- /dev/null
+++ b/gui.o
Binary files differ
diff --git a/libgfx.a b/libgfx.a
new file mode 100644
index 0000000..2964b8d
--- /dev/null
+++ b/libgfx.a
Binary files differ
diff --git a/libgui.a b/libgui.a
new file mode 100644
index 0000000..0b25307
--- /dev/null
+++ b/libgui.a
Binary files differ
diff --git a/makefile b/makefile
new file mode 100644
index 0000000..8572252
--- /dev/null
+++ b/makefile
@@ -0,0 +1,13 @@
+main : libgui.a libgfx.a
+libgfx.a : gfx.o
+ ar ru libgfx.a gfx.o
+ ranlib libgfx.a
+gfx.o : gfx.cpp
+ g++ -c gfx.cpp
+libgui.a : gui.o
+ ar ru libgui.a gui.o
+ ranlib libgui.a
+gui.o : gui.cpp
+ g++ -c gui.cpp
+clean :
+ rm gfx.a gui.a libgfx.a libgui.a gfx.o gui.o