gameui
changeset 0:3aa12cdb9925
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 24 Feb 2014 22:25:49 +0200 |
parents | |
children | 54ffb1765d39 |
files | Makefile include/button.h include/gameui.h include/label.h include/widget.h src/gameui_impl.h src/widget.cc test.cc |
diffstat | 8 files changed, 334 insertions(+), 0 deletions(-) [+] |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/Makefile Mon Feb 24 22:25:49 2014 +0200 1.3 @@ -0,0 +1,15 @@ 1.4 +src = test.cc $(wildcard src/*.cc) 1.5 +obj = $(src:.cc=.o) 1.6 + 1.7 +bin = test 1.8 + 1.9 +CFLAGS = -pedantic -Wall -g -Iinclude 1.10 +CXXFLAGS = $(CFLAGS) 1.11 +LDFLAGS = -lGL -lGLU -lglut 1.12 + 1.13 +$(bin): $(obj) 1.14 + $(CXX) -o $@ $(obj) $(LDFLAGS) 1.15 + 1.16 +.PHONY: clean 1.17 +clean: 1.18 + rm -f $(obj) $(bin)
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/include/button.h Mon Feb 24 22:25:49 2014 +0200 2.3 @@ -0,0 +1,21 @@ 2.4 +#ifndef GAMEUI_BUTTON_H_ 2.5 +#define GAMEUI_BUTTON_H_ 2.6 + 2.7 +#include "widget.h" 2.8 + 2.9 +namespace gameui { 2.10 + 2.11 +class ButtonImpl; 2.12 + 2.13 +class Button { 2.14 +private: 2.15 + ButtonImpl *impl; 2.16 + 2.17 +public: 2.18 + Button(); 2.19 + virtual ~Button(); 2.20 +}; 2.21 + 2.22 +} 2.23 + 2.24 +#endif // GAMEUI_BUTTON_H_
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/include/gameui.h Mon Feb 24 22:25:49 2014 +0200 3.3 @@ -0,0 +1,8 @@ 3.4 +#ifndef GAMEUI_H_ 3.5 +#define GAMEUI_H_ 3.6 + 3.7 +#include "widget.h" 3.8 +#include "button.h" 3.9 +#include "label.h" 3.10 + 3.11 +#endif // GAMEUI_H_
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/include/label.h Mon Feb 24 22:25:49 2014 +0200 4.3 @@ -0,0 +1,19 @@ 4.4 +#ifndef GAMEUI_LABEL_H_ 4.5 +#define GAMEUI_LABEL_H_ 4.6 + 4.7 +namespace gameui { 4.8 + 4.9 +class LabelImpl; 4.10 + 4.11 +class Label { 4.12 +private: 4.13 + LabelImpl *impl; 4.14 + 4.15 +public: 4.16 + Label(); 4.17 + virtual ~Label(); 4.18 +}; 4.19 + 4.20 +} 4.21 + 4.22 +#endif // GAMEUI_LABEL_H_
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/include/widget.h Mon Feb 24 22:25:49 2014 +0200 5.3 @@ -0,0 +1,63 @@ 5.4 +#ifndef GAMEUI_WIDGET_H_ 5.5 +#define GAMEUI_WIDGET_H_ 5.6 + 5.7 +namespace gameui { 5.8 + 5.9 +class Vec2 { 5.10 +public: 5.11 + float x, y; 5.12 + 5.13 + Vec2() : x(0), y(0) {} 5.14 + Vec2(float xx, float yy) : x(xx), y(yy) {} 5.15 +}; 5.16 + 5.17 +class BBox { 5.18 +public: 5.19 + Vec2 bmin, bmax; 5.20 +}; 5.21 + 5.22 +class WidgetImpl; 5.23 + 5.24 +class Widget { 5.25 +private: 5.26 + WidgetImpl *impl; 5.27 + 5.28 +public: 5.29 + enum VisState { 5.30 + VST_HIDDEN, 5.31 + VST_EASEIN, 5.32 + VST_VISIBLE, 5.33 + VST_EASEOUT 5.34 + }; 5.35 + enum ActiveState { 5.36 + AST_INACTIVE, 5.37 + AST_EASEIN, 5.38 + AST_ACTIVE, 5.39 + AST_EASEOUT 5.40 + }; 5.41 + 5.42 + Widget(); 5.43 + virtual ~Widget(); 5.44 + 5.45 + virtual void show(); 5.46 + virtual void hide(); 5.47 + virtual float get_visibility() const; 5.48 + 5.49 + virtual void activate(); 5.50 + virtual void deactivate(); 5.51 + virtual float get_active() const; 5.52 + 5.53 + virtual const BBox &get_box() const; 5.54 + virtual const Vec2 &get_position() const; 5.55 + virtual const Vec2 &get_size() const; 5.56 + 5.57 + virtual bool hit_test(const Vec2 &pt) const; 5.58 + 5.59 + virtual void draw() const = 0; 5.60 +}; 5.61 + 5.62 +long get_cur_time(); 5.63 + 5.64 +} 5.65 + 5.66 +#endif // GAMEUI_WIDGET_H_
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/src/gameui_impl.h Mon Feb 24 22:25:49 2014 +0200 6.3 @@ -0,0 +1,4 @@ 6.4 +#ifndef GAMEUI_IMPL_H_ 6.5 +#define GAMEUI_IMPL_H_ 6.6 + 6.7 +#endif // GAMEUI_IMPL_H_
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/src/widget.cc Mon Feb 24 22:25:49 2014 +0200 7.3 @@ -0,0 +1,110 @@ 7.4 +#include <string> 7.5 +#include <stringstream> 7.6 +#include "widget.h" 7.7 + 7.8 +using namespace gameui; 7.9 + 7.10 +class WidgetImpl { 7.11 +public: 7.12 + std::string name_prefix; 7.13 + std::string name; 7.14 + BBox box; 7.15 + VisState vis_st; 7.16 + ActiveState act_st; 7.17 + 7.18 + float vis, act; 7.19 + 7.20 + long vis_start_time, act_start_time; 7.21 +}; 7.22 + 7.23 + 7.24 +Widget::Widget() 7.25 + : name_prefix("widget") 7.26 +{ 7.27 + static int widget_count; 7.28 + 7.29 + std::stringstream sstr; 7.30 + sstr << name_prefix << widget_count++; 7.31 + name = sstr.str(); 7.32 + 7.33 + box.bmin = Vec2(0, 0); 7.34 + box.bmax = Vec2(1, 1); 7.35 + 7.36 + vis_st = VST_VISIBLE; 7.37 + act_st = AST_ACTIVE; 7.38 + 7.39 + vis = act = 1.0; 7.40 +} 7.41 + 7.42 +Widget::~Widget() 7.43 +{ 7.44 +} 7.45 + 7.46 +void Widget::show() 7.47 +{ 7.48 + if(vis_st == VST_EASEVISIBLE || vis_st == VST_EASEIN) { 7.49 + return; 7.50 + } 7.51 + 7.52 + vis_st = VST_EASEIN; 7.53 + vis_start_time = get_cur_time(); 7.54 +} 7.55 + 7.56 +void Widget::hide() 7.57 +{ 7.58 + if(vis_st == VST_EASEHIDDEN || vis_st == VST_EASEOUT) { 7.59 + return; 7.60 + } 7.61 + 7.62 + vis_st = VST_EASEOUT; 7.63 + vis_start_time = get_cur_time(); 7.64 +} 7.65 + 7.66 +float Widget::get_visibility() const 7.67 +{ 7.68 + switch(vis_st) { 7.69 + case VST_EASEIN: 7.70 + vis = (get_cur_time() - vis_start_time) / gameui::ease_time; 7.71 + if(vis < 0.0) vis = 0.0; 7.72 + if(vis > 1.0) vis = 1.0; 7.73 + break; 7.74 + 7.75 + case VST_EASEOUT: 7.76 + vis = 1.0 - (get_cur_time() - vis_start_time) / gameui::ease_time; 7.77 + if(vis < 0.0) vis = 0.0; 7.78 + if(vis > 1.0) vis = 1.0; 7.79 + break; 7.80 + 7.81 + case VST_HIDDEN: 7.82 + vis = 0.0; 7.83 + break; 7.84 + 7.85 + case VST_VISIBLE: 7.86 + vis = 1.0; 7.87 + break; 7.88 + } 7.89 + 7.90 + return vis; 7.91 +} 7.92 + 7.93 +#ifdef WIN32 7.94 +long gameui::get_cur_time() 7.95 +{ 7.96 + return GetTickCount(); 7.97 +} 7.98 +#endif 7.99 + 7.100 +#if defined(__unix__) || defined(__APPLE__) 7.101 +long gameui::get_cur_time() 7.102 +{ 7.103 + struct timeval tv; 7.104 + static struct timeval tv0; 7.105 + 7.106 + gettimeofday(&tv, 0); 7.107 + if(tv0.tv_sec == 0 && tv0.tv_msec == 0) { 7.108 + tv0 = tv; 7.109 + return 0; 7.110 + } 7.111 + return (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000; 7.112 +} 7.113 +#endif
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 8.2 +++ b/test.cc Mon Feb 24 22:25:49 2014 +0200 8.3 @@ -0,0 +1,94 @@ 8.4 +#include <stdio.h> 8.5 +#include <stdlib.h> 8.6 +#include <assert.h> 8.7 +#include <GL/glut.h> 8.8 + 8.9 +static bool init(); 8.10 +static void cleanup(); 8.11 +static void disp(); 8.12 +static void reshape(int x, int y); 8.13 +static void keypress(unsigned char key, int x, int y); 8.14 +static void keyrelease(unsigned char key, int x, int y); 8.15 +static void skeypress(int key, int x, int y); 8.16 +static void skeyrelease(int key, int x, int y); 8.17 +static void mouse(int bn, int st, int x, int y); 8.18 +static void motion(int x, int y); 8.19 + 8.20 +int main(int argc, char **argv) 8.21 +{ 8.22 + glutInitWindowSize(800, 600); 8.23 + glutInit(&argc, argv); 8.24 + glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); 8.25 + glutCreateWindow("gameui test"); 8.26 + 8.27 + glutDisplayFunc(disp); 8.28 + glutReshapeFunc(reshape); 8.29 + glutKeyboardFunc(keypress); 8.30 + glutKeyboardUpFunc(keyrelease); 8.31 + glutSpecialFunc(skeypress); 8.32 + glutSpecialUpFunc(skeyrelease); 8.33 + glutMouseFunc(mouse); 8.34 + glutMotionFunc(motion); 8.35 + glutPassiveMotionFunc(motion); 8.36 + 8.37 + if(!init()) { 8.38 + return 1; 8.39 + } 8.40 + atexit(cleanup); 8.41 + 8.42 + glutMainLoop(); 8.43 + return 0; 8.44 +} 8.45 + 8.46 + 8.47 +static bool init() 8.48 +{ 8.49 + return true; 8.50 +} 8.51 + 8.52 +static void cleanup() 8.53 +{ 8.54 +} 8.55 + 8.56 +static void disp() 8.57 +{ 8.58 + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 8.59 + 8.60 + glutSwapBuffers(); 8.61 + assert(glGetError() == GL_NO_ERROR); 8.62 +} 8.63 + 8.64 +static void reshape(int x, int y) 8.65 +{ 8.66 + glViewport(0, 0, x, y); 8.67 + glMatrixMode(GL_PROJECTION); 8.68 + glLoadIdentity(); 8.69 + glOrtho(0, x, y, 0, -1, 1); 8.70 +} 8.71 + 8.72 +static void keypress(unsigned char key, int x, int y) 8.73 +{ 8.74 + if(key == 27) { 8.75 + exit(0); 8.76 + } 8.77 +} 8.78 + 8.79 +static void keyrelease(unsigned char key, int x, int y) 8.80 +{ 8.81 +} 8.82 + 8.83 +static void skeypress(int key, int x, int y) 8.84 +{ 8.85 +} 8.86 + 8.87 +static void skeyrelease(int key, int x, int y) 8.88 +{ 8.89 +} 8.90 + 8.91 +static void mouse(int bn, int st, int x, int y) 8.92 +{ 8.93 +} 8.94 + 8.95 +static void motion(int x, int y) 8.96 +{ 8.97 +}