]> git.lizzy.rs Git - dragonblocks-bedrock.git/blob - src/graphics.cpp
Add files via upload
[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
10 position Graphics::pointed;
11 position Graphics::pos = {MAPWIDTH/2 - DISPLAYWIDTH/2, MAPHEIGHT/2 - DISPLAYHEIGHT/2};
12
13 using namespace std;
14 void Graphics::display(){
15         glClear(GL_COLOR_BUFFER_BIT);
16         //sky
17         drawRectangle(0, 0, DISPLAYWIDTH*BLOCKWIDTH, DISPLAYHEIGHT*BLOCKWIDTH, "#87CEEB");
18         //map
19         for(int x = 0; x < DISPLAYWIDTH; x++){
20                 for(int y = 0; y < DISPLAYHEIGHT; y++){
21                         Game::map -> getNode(x+pos.x, y+pos.y) -> texture -> draw(x*BLOCKWIDTH, y*BLOCKWIDTH, BLOCKWIDTH, BLOCKWIDTH);
22                 }
23         }
24         //pointed block
25         if(pointed.x < DISPLAYWIDTH){
26                 drawRectangle(pointed.x * BLOCKWIDTH, pointed.y * BLOCKWIDTH, BLOCKWIDTH, 1, COLOR_BLACK);
27                 drawRectangle(pointed.x * BLOCKWIDTH + BLOCKWIDTH - 1, pointed.y * BLOCKWIDTH, 1, BLOCKWIDTH, COLOR_BLACK);
28                 drawRectangle(pointed.x * BLOCKWIDTH, pointed.y * BLOCKWIDTH + BLOCKWIDTH - 1, BLOCKWIDTH, 1, COLOR_BLACK);
29                 drawRectangle(pointed.x * BLOCKWIDTH, pointed.y * BLOCKWIDTH, 1, BLOCKWIDTH, COLOR_BLACK);
30         }
31         //inventory
32         
33         drawRectangle(DISPLAYWIDTH*BLOCKWIDTH, 0, INVWIDTH, DISPLAYHEIGHT*BLOCKWIDTH, "#B4B4B4");
34         drawRectangle(DISPLAYWIDTH*BLOCKWIDTH, Game::inventory->selected * INVWIDTH, INVWIDTH, INVWIDTH, "#636363");
35         for(int i = 0; i < Game::inventory->count; i++)
36                 Game::inventory -> getSlot(i) -> texture -> draw(BLOCKWIDTH*DISPLAYWIDTH + (INVWIDTH-INVBLOCKWIDTH)/2, i * INVWIDTH + (INVWIDTH-INVBLOCKWIDTH)/2, INVBLOCKWIDTH, INVBLOCKWIDTH);
37         //infotext
38         writeText(5, 5, (string)"Dragonblocks "+VERSION, GLUT_BITMAP_9_BY_15, COLOR_WHITE);
39         string infotext = "pos: ("+to_string(pos.x)+", "+to_string(pos.y)+"), seed: "+to_string(Game::seed);
40         if(pointed.x < DISPLAYWIDTH)
41                 infotext += ", pointed: "+ Game::map->getNode(pointed.x+pos.x, pointed.y+pos.y)->name + "("+to_string(pointed.x+pos.x)+", "+to_string(pointed.y+pos.y)+")";
42         writeText(5, 20, infotext, GLUT_BITMAP_9_BY_15, COLOR_WHITE);
43         //writeText(5, 35, "world: "+Game::mapfile, GLUT_BITMAP_9_BY_15, COLOR_WHITE);
44         glFlush();
45 }
46 void Graphics::reshape(int width, int height){
47         glViewport(0, 0, width, height);       /* Establish viewing area to cover entire window. */
48         glMatrixMode(GL_PROJECTION);  /* Start modifying the projection matrix. */
49         glLoadIdentity();             /* Reset project matrix. */
50         glOrtho(0, width, 0, height, -1, 1);   /* Map abstract coords directly to window coords. */
51         glScalef(1, -1, 1);           /* Invert Y axis so increasing Y goes down. */
52         glTranslatef(0, -height, 0);       /* Shift origin up to upper-pos.x corner. */
53 }
54 void Graphics::init(){
55         glutInit(Game::argc, Game::argv);
56         glutCreateWindow("Dragonblocks");
57         glutReshapeWindow(DISPLAYWIDTH*BLOCKWIDTH+INVWIDTH, DISPLAYHEIGHT*BLOCKWIDTH);
58         glutDisplayFunc(display);
59         glutReshapeFunc(reshape);
60         glutKeyboardFunc(keyboard);
61         glutSpecialFunc(special);
62         glutMouseFunc(mouse);
63         glutPassiveMotionFunc(motion);
64         glutMotionFunc(motion);
65         glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);
66         glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
67         glutMainLoop();
68 }
69 void Graphics::keyboard(unsigned char key, int x, int y){
70 }
71 void Graphics::special(int key, int x, int y){
72         switch(key){
73                 case GLUT_KEY_LEFT:
74                         if(pos.x > 0)
75                                 pos.x--;
76                         break;
77                 case GLUT_KEY_UP:
78                         if(pos.y > 0)
79                                 pos.y--;
80                         break;
81                 case GLUT_KEY_RIGHT:
82                         if(pos.x < MAPWIDTH-DISPLAYWIDTH)
83                                 pos.x++;
84                         break;
85                 case GLUT_KEY_DOWN:
86                         if(pos.y < MAPHEIGHT-DISPLAYHEIGHT)
87                                 pos.y++;
88                         break;
89         }
90 }
91 void Graphics::mouse(int key, int action, int x, int y){
92         if(action == 0){
93                 if(x < BLOCKWIDTH*DISPLAYWIDTH){
94                         switch(key){
95                                 case 0:
96                                         if(Game::map -> getNode(pointed.x+pos.x, pointed.y+pos.y)->stable)
97                                                 Game::map -> setNode(pointed.x+pos.x, pointed.y+pos.y, MAPGEN_AIR);
98                                         break;
99                                 case 2:
100                                         if(! Game::map -> getNode(pointed.x+pos.x, pointed.y+pos.y)->stable)
101                                                 Game::map -> setNode(pointed.x+pos.x, pointed.y+pos.y, Game::inventory->getSelectedSlot());
102                                         break;
103                         }
104                 }
105                 else{
106                         Game::inventory->select(y/INVWIDTH);
107                 }
108         }
109 }
110 void Graphics::motion(int x, int y){
111         pointed.x = x / BLOCKWIDTH;
112         pointed.y = y / BLOCKWIDTH;
113 }
114 void Graphics::writeText(int x, int y, string text, void *font, color c){
115         glColor3f(c.red, c.green, c.blue);
116         glRasterPos2i(x, y+10);
117         char *s = strdup(text.c_str());
118         --s;
119         while(*++s)
120                 glutBitmapCharacter(font, *s);
121 }
122 void Graphics::drawRectangle(int x, int y, int width, int height, color c){
123         glColor3f(c.red, c.green, c.blue);
124         glBegin(GL_POLYGON);
125         glVertex2i(x, y);
126         glVertex2i(x+width, y);
127         glVertex2i(x+width, y+height);
128         glVertex2i(x, y+height);
129         glEnd();
130 }