]> git.lizzy.rs Git - dragonblocks-bedrock.git/blob - src/graphics.cpp
Some structure Changes
[dragonblocks-bedrock.git] / src / graphics.cpp
1 #include <GL/freeglut.h>
2 #include <cstring>
3 #include <string>
4 #include <iostream>
5 #include "graphics.h"
6 #include "util.h"
7 #include "game.h"
8 #include "mapgen.h"
9 #include "entity.h"
10
11 position Graphics::pointed;
12 position Graphics::scroll = {MAPWIDTH/2 - DISPLAYWIDTH/2, MAPHEIGHT/2 - DISPLAYHEIGHT/2};
13 int Graphics::inventory_scroll;
14
15 using namespace std;
16 void Graphics::drawSky(){
17         drawRectangle(0, 0, DISPLAYWIDTH*BLOCKWIDTH, DISPLAYHEIGHT*BLOCKWIDTH, "#87CEEB");
18 }
19 void Graphics::drawMap(){
20         for(int x = 0; x < DISPLAYWIDTH; x++){
21                 for(int y = 0; y < DISPLAYHEIGHT; y++){
22                         Game::map -> getNode(x+scroll.x, y+scroll.y) -> texture -> draw(x*BLOCKWIDTH, y*BLOCKWIDTH, BLOCKWIDTH, BLOCKWIDTH);
23                 }
24         }
25 }
26 void Graphics::drawEntities(){
27         for(int i = 0; i < Entity::count; i++){
28                 if(Entity::list[i] == NULL)
29                         continue;
30                 Entity *entity = Entity::list[i];
31                 entity -> texture -> draw((entity->x - scroll.x) * BLOCKWIDTH, (entity->y - scroll.y) * BLOCKWIDTH, entity->width * BLOCKWIDTH, entity->height * BLOCKWIDTH);   
32         }
33 }
34 void Graphics::drawPointedBlock(){
35         if(pointed.x < DISPLAYWIDTH){
36                 drawRectangle(pointed.x * BLOCKWIDTH, pointed.y * BLOCKWIDTH, BLOCKWIDTH, 1, COLOR_BLACK);
37                 drawRectangle(pointed.x * BLOCKWIDTH + BLOCKWIDTH - 1, pointed.y * BLOCKWIDTH, 1, BLOCKWIDTH, COLOR_BLACK);
38                 drawRectangle(pointed.x * BLOCKWIDTH, pointed.y * BLOCKWIDTH + BLOCKWIDTH - 1, BLOCKWIDTH, 1, COLOR_BLACK);
39                 drawRectangle(pointed.x * BLOCKWIDTH, pointed.y * BLOCKWIDTH, 1, BLOCKWIDTH, COLOR_BLACK);
40         }
41 }
42 void Graphics::drawInventory(){
43         Inventory *inv = Game::player->inventory;
44         int x = DISPLAYWIDTH*BLOCKWIDTH;
45         drawRectangle(x, 0, INVWIDTH, DISPLAYHEIGHT*BLOCKWIDTH, "#B4B4B4");
46         drawRectangle(x, (inv->selected - inventory_scroll) * INVWIDTH, INVWIDTH, INVWIDTH, "#636363");
47         x = BLOCKWIDTH*DISPLAYWIDTH + (INVWIDTH-INVBLOCKWIDTH)/2;
48         int yf = (INVWIDTH-INVBLOCKWIDTH)/2;
49         for(int i = 0; i < Game::player->inventory->count; i++){
50                 int nr = i + inventory_scroll;
51                 if(inv -> getSlot(nr))
52                         inv -> getSlot(nr) -> texture -> draw(x, i * INVWIDTH + yf, INVBLOCKWIDTH, INVBLOCKWIDTH);
53                 else
54                         drawRectangle(x, i * INVWIDTH + yf, INVBLOCKWIDTH, INVBLOCKWIDTH, "#3E3E3E");
55         }
56 }
57 void Graphics::drawInfotext(){
58         writeText(5, 5, (string)"Dragonblocks "+VERSION, GLUT_BITMAP_9_BY_15, COLOR_WHITE);
59         string infotext = "pos: ("+to_string(Game::player->x)+", "+to_string(Game::player->y)+") | seed: "+to_string(Game::seed);
60         if(pointed.x < DISPLAYWIDTH)
61                 infotext += " | pointed: "+ Game::map->getNode(pointed.x+scroll.x, pointed.y+scroll.y)->name + "("+to_string(pointed.x+scroll.x)+", "+to_string(pointed.y+scroll.y)+")";
62         writeText(5, 20, infotext, GLUT_BITMAP_9_BY_15, COLOR_WHITE);
63 }
64 void Graphics::display(){
65         glClear(GL_COLOR_BUFFER_BIT);
66         drawSky();
67         drawEntities();
68         drawMap();
69         drawPointedBlock();
70         drawInventory();
71         drawInfotext();
72         glFlush();
73 }
74 void Graphics::reshape(int width, int height){
75         glViewport(0, 0, width, height);
76         glMatrixMode(GL_PROJECTION);
77         glLoadIdentity();
78         glOrtho(0, width, 0, height, -1, 1);
79         glScalef(1, -1, 1);
80         glTranslatef(0, -height, 0);
81 }
82 void Graphics::init(){
83         glutInit(Game::argc, Game::argv);
84         glutCreateWindow("Dragonblocks");
85         glutReshapeWindow(DISPLAYWIDTH*BLOCKWIDTH+INVWIDTH, DISPLAYHEIGHT*BLOCKWIDTH);
86         glutDisplayFunc(display);
87         glutReshapeFunc(reshape);
88         glutKeyboardFunc(keyboard);
89         glutSpecialFunc(special);
90         glutSpecialUpFunc(special_up);
91         glutMouseFunc(mouse);
92         glutPassiveMotionFunc(motion);
93         glutMotionFunc(motion);
94         glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);
95         glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF);
96         glutMainLoop();
97 }
98 void Graphics::keyboard(unsigned char key, int x, int y){
99         switch(key){
100                 case ' ':
101                         Game::player->jump();
102                         break;
103         }
104 }
105 void Graphics::special(int key, int x, int y){
106         switch(key){
107                 case GLUT_KEY_LEFT:
108                         Game::player->left();
109                         break;
110                 case GLUT_KEY_RIGHT:
111                         Game::player->right();
112                         break;
113                 case GLUT_KEY_UP:
114                         Game::player->inventory->selectUp();
115                         ajustInventoryScroll();
116                         break;
117                 case GLUT_KEY_DOWN:
118                         Game::player->inventory->selectDown();
119                         ajustInventoryScroll();
120                         break;
121         }
122 }
123 void Graphics::special_up(int key, int x, int y){
124         switch(key){
125                 case GLUT_KEY_LEFT:
126                 case GLUT_KEY_RIGHT:
127                         Game::player->stop();
128                         break;
129         }
130 }
131 void Graphics::ajustScroll(){
132         if(Game::player->y - scroll.y < 2)
133                 if(scroll.y > 0)
134                         scroll.y--;
135         if(Game::player->x - scroll.x < 2)
136                 if(scroll.x > 0)
137                         scroll.x--;
138         if(scroll.y + DISPLAYHEIGHT - Game::player->y - Game::player->height < 2)
139                 if(scroll.y < MAPHEIGHT-DISPLAYHEIGHT)
140                         scroll.y++;
141         if(scroll.x + DISPLAYWIDTH - Game::player->x - Game::player->width < 2)
142                 if(scroll.x < MAPWIDTH-DISPLAYWIDTH)
143                         scroll.x++;
144 }
145 void Graphics::ajustInventoryScroll(){
146         Inventory *inv = Game::player->inventory;
147         int invblocks_displayed = DISPLAYHEIGHT * BLOCKWIDTH / INVWIDTH;
148         if(inv->selected < inventory_scroll)
149                 inventory_scroll = inv->selected;
150         else if(inv->selected >= inventory_scroll + invblocks_displayed)
151                 inventory_scroll = inv->selected - invblocks_displayed + 1;
152 }
153 void Graphics::mouse(int key, int action, int x, int y){
154         if(action == 0){
155                 if(x < BLOCKWIDTH*DISPLAYWIDTH){
156                         switch(key){
157                                 case 0:
158                                         if(Game::map -> getNode(pointed.x+scroll.x, pointed.y+scroll.y)->stable)
159                                                 Game::map -> setNode(pointed.x+scroll.x, pointed.y+scroll.y, MAPGEN_AIR);
160                                         break;
161                                 case 2:
162                                         if(! Game::map -> getNode(pointed.x+scroll.x, pointed.y+scroll.y)->stable)
163                                                 Game::map -> setNode(pointed.x+scroll.x, pointed.y+scroll.y, Game::player->inventory->getSelectedSlot());
164                                         break;
165                         }
166                 }
167                 else{
168                         Game::player->inventory->select(y/INVWIDTH + inventory_scroll);
169                 }
170         }
171 }
172 void Graphics::motion(int x, int y){
173         pointed.x = x / BLOCKWIDTH;
174         pointed.y = y / BLOCKWIDTH;
175 }
176 void Graphics::writeText(int x, int y, string text, void *font, color c){
177         glColor3f(c.red, c.green, c.blue);
178         glRasterPos2i(x, y+10);
179         char *s = strdup(text.c_str());
180         --s;
181         while(*++s)
182                 glutBitmapCharacter(font, *s);
183 }
184 void Graphics::drawRectangle(int x, int y, int width, int height, color c){
185         glColor3f(c.red, c.green, c.blue);
186         glBegin(GL_POLYGON);
187         glVertex2i(x, y);
188         glVertex2i(x+width, y);
189         glVertex2i(x+width, y+height);
190         glVertex2i(x, y+height);
191         glEnd();
192 }