graphics

experiments with opengl3.2/ogles3.3 on linux and win7
git clone http://frotz.net/git/graphics.git
Log | Files | Refs

menu.cc (3598B)


      1 /* Copyright 2013 Brian Swetland <swetland@frotz.net>
      2  *
      3  * Licensed under the Apache License, Version 2.0 (the "License");
      4  * you may not use this file except in compliance with the License.
      5  * You may obtain a copy of the License at
      6  *
      7  *     http://www.apache.org/licenses/LICENSE-2.0
      8  *
      9  * Unless required by applicable law or agreed to in writing, software
     10  * distributed under the License is distributed on an "AS IS" BASIS,
     11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12  * See the License for the specific language governing permissions and
     13  * limitations under the License.
     14  */
     15 
     16 #include "app.h"
     17 #include "matrix.h"
     18 #include "shared.h"
     19 
     20 #include "texturefont.h"
     21 #include "textgrid.h"
     22 #include "Renderable.h"
     23 
     24 struct MenuItem {
     25 	MenuItem *next;
     26 	const char *text;
     27 	void *cookie;
     28 	unsigned x, y;
     29 	unsigned width;
     30 	unsigned t, b, l, r;
     31 };
     32 
     33 class BigMenu {
     34 public:
     35 	void addItem(const char *text, void *cookie);
     36 	void layout(unsigned width, unsigned height);
     37 	void update(unsigned x, unsigned y);
     38 	void render(void) { text->render(); }
     39 	static BigMenu *create(TextureFont *font);
     40 private:
     41 	DISALLOW_COPY_AND_ASSIGN(BigMenu);
     42 	BigMenu(TextureFont *_font);
     43 	TextureFont *font;
     44 	Text *text;
     45 	MenuItem *first, *last;
     46 	unsigned count;
     47 };
     48 
     49 BigMenu* BigMenu::create(TextureFont *font) {
     50 	return new BigMenu(font);
     51 }
     52 
     53 BigMenu::BigMenu(TextureFont *_font)
     54 	: font(_font), first(nullptr), last(nullptr), count(0) {
     55 	text = Text::create(_font);
     56 }
     57 
     58 void BigMenu::addItem(const char *text, void *cookie) {
     59 	MenuItem *itm = new MenuItem;
     60 	itm->next = nullptr;
     61 	itm->text = text;
     62 	itm->cookie = cookie;
     63 	itm->width = 0;
     64 	itm->x = 0;
     65 	itm->y = 0;
     66 	if (last)
     67 		last->next = itm;
     68 	else
     69 		first = itm;
     70 	last = itm;
     71 	count++;
     72 }
     73 
     74 void BigMenu::layout(unsigned width, unsigned height) {
     75 	unsigned y, skip;
     76 	skip = (font->getMaxHeight() * 5) / 3;
     77 	y = (height / 2) - ((skip * count) / 2) + font->getMaxAscent();
     78 	for (MenuItem *itm = first; itm; itm = itm->next) {
     79 		unsigned iw, ih;
     80 		font->measure(itm->text, &iw, &ih);
     81 		itm->y = y;
     82 		itm->x = (width / 2) - (iw / 2);
     83 		printf("iw %d\n", iw);
     84 		itm->width = iw;
     85 		text->puts(itm->x, itm->y, itm->text);
     86 		y += skip;
     87 
     88 		itm->t = itm->y - font->getMaxAscent();
     89 		itm->b = itm->t + font->getMaxHeight();
     90 		itm->l = itm->x;
     91 		itm->r = itm->x + itm->width;
     92 	}
     93 }
     94 
     95 void BigMenu::update(unsigned x, unsigned y) {
     96 	text->clear();
     97 	for (MenuItem *itm = first; itm; itm = itm->next) {
     98 		if ((x > itm->l) && (x < itm->r) && (y > itm->t) && (y < itm->b))
     99 			text->setColor(RGBA(0,255,0,255));
    100 		else
    101 			text->setColor(RGBA(255,255,255,255));
    102 		text->puts(itm->x, itm->y, itm->text);
    103 	}
    104 }
    105 
    106 class TestApp : public App {
    107 public:
    108 	TestApp();
    109 	int init(void);
    110 	void render(void);
    111 
    112 private:
    113 	UniformBuffer obj, mat, scn;
    114 	mat4 proj;
    115 
    116 	TextureFont *font;
    117 	BigMenu *menu;
    118 };
    119 
    120 TestApp::TestApp() : App() { }
    121 
    122 int TestApp::init(void) {
    123 	if (!(font = TextureFont::load("orbitron-bold-72")))
    124 		return error("cannot load font");
    125 	menu = BigMenu::create(font);
    126 	menu->addItem("Start", nullptr);
    127 	menu->addItem("Options", nullptr);
    128 	menu->addItem("Quit", nullptr);
    129 	menu->layout(width, height);
    130 	return 0;
    131 }
    132 
    133 void TestApp::render(void) {
    134 	struct ubScene scene;
    135 	struct ubObject object;
    136 	struct ubMaterial material;
    137 	mat4 model, view, tmp;
    138 
    139 	scene.Ortho.setOrtho(0, width, height, 0, -1.0, 1.0);
    140 	scene.OrthoSize.set(width, height, 0, 0);
    141 	scene.TextGrid.set(16, 16, 64, 64);
    142 	scn.load(&scene, sizeof(scene));
    143 	scn.use(U_SCENE);
    144 
    145 	int mx, my;
    146 	SDL_GetMouseState(&mx,&my);
    147 	menu->update(mx, my);
    148 	menu->render();
    149 }
    150 
    151 App *createApp(void) {
    152 	return new TestApp();
    153 }