1 #include <GL/freeglut.h>
10 position Graphics::pointed;
11 position Graphics::pos = {MAPWIDTH/2 - DISPLAYWIDTH/2, MAPHEIGHT/2 - DISPLAYHEIGHT/2};
14 void Graphics::display(){
15 glClear(GL_COLOR_BUFFER_BIT);
17 drawRectangle(0, 0, DISPLAYWIDTH*BLOCKWIDTH, DISPLAYHEIGHT*BLOCKWIDTH, "#87CEEB");
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);
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);
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);
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);
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. */
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);
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);
69 void Graphics::keyboard(unsigned char key, int x, int y){
71 void Graphics::special(int key, int x, int y){
82 if(pos.x < MAPWIDTH-DISPLAYWIDTH)
86 if(pos.y < MAPHEIGHT-DISPLAYHEIGHT)
91 void Graphics::mouse(int key, int action, int x, int y){
93 if(x < BLOCKWIDTH*DISPLAYWIDTH){
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);
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());
106 Game::inventory->select(y/INVWIDTH);
110 void Graphics::motion(int x, int y){
111 pointed.x = x / BLOCKWIDTH;
112 pointed.y = y / BLOCKWIDTH;
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());
120 glutBitmapCharacter(font, *s);
122 void Graphics::drawRectangle(int x, int y, int width, int height, color c){
123 glColor3f(c.red, c.green, c.blue);
126 glVertex2i(x+width, y);
127 glVertex2i(x+width, y+height);
128 glVertex2i(x, y+height);