1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>

#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <GL/gl.h>
#include <GL/glu.h>

#define screen_width 640
#define screen_height 480

int app_state = 1, app_exit = 1;
    SDL_Surface *screen;
    GLuint menu_image;
    SDL_Event control_event;

    GLuint LoadTexture(char *filename,int textw,int texth) {
            SDL_Surface *surface;
            GLuint textureid;
            int mode;
            surface = IMG_Load(filename);
            // Or if you don't use SDL_image you can use SDL_LoadBMP here instead:
            // surface = SDL_LoadBMP(filename);
            // could not load filename
            if (!surface) { return 0; }
            // work out what format to tell glTexImage2D to use...
            if (surface->format->BytesPerPixel == 3) { // RGB 24bit
                    mode = GL_RGB;
            } else if (surface->format->BytesPerPixel == 4) { // RGBA 32bit
                    mode = GL_RGBA;
            } else {
                    SDL_FreeSurface(surface);
                    return 0;
            }
             textw=surface->w;
             texth=surface->h;
            // create one texture name
            glGenTextures(1, &textureid);
            // tell opengl to use the generated texture name
            glBindTexture(GL_TEXTURE_2D, textureid);
            // this reads from the sdl surface and puts it into an opengl texture
            glTexImage2D(GL_TEXTURE_2D, 0, mode, surface->w, surface->h, 0, mode, GL_UNSIGNED_BYTE, surface->pixels);
            // these affect how this texture is drawn later on...
            glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
            glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
            // clean up
            SDL_FreeSurface(surface);
            return textureid;
    }

    void DrawTexture(int x, int y, int textw,int texth, GLuint textureid) {
        // planning to add x,y,texture width,texture height to the function
        glBindTexture(GL_TEXTURE_2D, textureid);
        glBegin(GL_QUADS);
            glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
            glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
            glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
            glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
        glEnd();
    }

    static void keydown_event(SDL_keysym* key) {

        switch(app_state) {

            case 1:
                switch(key->sym) {
                    case SDLK_ESCAPE:
                        app_exit = 0; // exits application
                    break;
                    default:
                        break;
                }
            break;

            case 2:
                switch(key->sym) {
                    case SDLK_ESCAPE:
                        app_exit = 0; // exits application
                    break;
                    default:
                        break;
                }
            break;

        }

    }
int main(int argc, char* args[]) {
            printf("Starting Graphics engine\n");
            SDL_Init( SDL_INIT_EVERYTHING );
            screen = SDL_SetVideoMode( screen_width, screen_height, 32, SDL_HWSURFACE | SDL_GL_DOUBLEBUFFER | SDL_OPENGL );
            SDL_WM_SetCaption("Dark Galaxy",NULL);
            printf("Setting up opengl settings and functions\n");

            glEnable(GL_TEXTURE_2D);
            glShadeModel(GL_SMOOTH);
            glClearColor(0.0f,0.0f,0.0f,0.5f);
            glClearDepth(1.0f);
            glEnable(GL_DEPTH_TEST);
            glDepthFunc(GL_LEQUAL);
            glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

            glViewport(0,0,screen_width,screen_height);

            printf("Finished setting up opengl settings and functions\n");




            menu_image = LoadTexture("Resource/menu.png",640,480);
            if(menu_image) { printf("Loaded Image\n"); }else{ printf("Failed to load\n"); }


            while(app_exit == 1) {
                while(SDL_PollEvent(&control_event)) {
                    switch(control_event.type) {
                        case SDL_KEYDOWN:
                            keydown_event(&control_event.key.keysym);
                        break;
                        case SDL_QUIT:
                            app_exit = 0; // exits application
                        break;
                    }
                }
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
                glLoadIdentity();

                    switch(app_state) {

                    case 1:
                        DrawTexture(0, 0, 640, 480, menu_image);
                    break;

                    case 2:

                    break;

                }

                SDL_GL_SwapBuffers();
            }






            printf("Exiting Graphics engine\n");
            SDL_FreeSurface(screen);
            SDL_Quit();

        return 0;
}