1 #include <GL/freeglut.h>
11 position Graphics::pointed;
12 position Graphics::scroll = {MAPWIDTH/2 - DISPLAYWIDTH/2, MAPHEIGHT/2 - DISPLAYHEIGHT/2};
13 int Graphics::inventory_scroll;
16 void Graphics::drawSky(){
17 drawRectangle(0, 0, DISPLAYWIDTH*BLOCKWIDTH, DISPLAYHEIGHT*BLOCKWIDTH, "#87CEEB");
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);
26 void Graphics::drawEntities(){
27 for(int i = 0; i < Entity::count; i++){
28 if(Entity::list[i] == NULL)
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);
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);
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);
54 drawRectangle(x, i * INVWIDTH + yf, INVBLOCKWIDTH, INVBLOCKWIDTH, "#3E3E3E");
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);
64 void Graphics::display(){
65 glClear(GL_COLOR_BUFFER_BIT);
74 void Graphics::reshape(int width, int height){
75 glViewport(0, 0, width, height);
76 glMatrixMode(GL_PROJECTION);
78 glOrtho(0, width, 0, height, -1, 1);
80 glTranslatef(0, -height, 0);
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);
92 glutPassiveMotionFunc(motion);
93 glutMotionFunc(motion);
94 glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);
95 glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF);
98 void Graphics::keyboard(unsigned char key, int x, int y){
101 Game::player->jump();
105 void Graphics::special(int key, int x, int y){
108 Game::player->left();
111 Game::player->right();
114 Game::player->inventory->selectUp();
115 ajustInventoryScroll();
118 Game::player->inventory->selectDown();
119 ajustInventoryScroll();
123 void Graphics::special_up(int key, int x, int y){
127 Game::player->stop();
131 void Graphics::ajustScroll(){
132 if(Game::player->y - scroll.y < 2)
135 if(Game::player->x - scroll.x < 2)
138 if(scroll.y + DISPLAYHEIGHT - Game::player->y - Game::player->height < 2)
139 if(scroll.y < MAPHEIGHT-DISPLAYHEIGHT)
141 if(scroll.x + DISPLAYWIDTH - Game::player->x - Game::player->width < 2)
142 if(scroll.x < MAPWIDTH-DISPLAYWIDTH)
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;
153 void Graphics::mouse(int key, int action, int x, int y){
155 if(x < BLOCKWIDTH*DISPLAYWIDTH){
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);
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());
168 Game::player->inventory->select(y/INVWIDTH + inventory_scroll);
172 void Graphics::motion(int x, int y){
173 pointed.x = x / BLOCKWIDTH;
174 pointed.y = y / BLOCKWIDTH;
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());
182 glutBitmapCharacter(font, *s);
184 void Graphics::drawRectangle(int x, int y, int width, int height, color c){
185 glColor3f(c.red, c.green, c.blue);
188 glVertex2i(x+width, y);
189 glVertex2i(x+width, y+height);
190 glVertex2i(x, y+height);