summaryrefslogtreecommitdiffstats
path: root/sdl/pyramid.c
blob: ff8a03f6a14bfc4cb34068e7d196e22c7ada7d91 (plain)
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
157
158
159
/* pyramid.c
 *
 * @2011 Kamil Kaminski
 *
 * notes: since lighting is enabled, we need to specify normals for
 * each polygon face so the OpenGL can calculate e.g. how light reflects
 *
 * drawing a shadow for the pyramid would require drawing things twice, so on 2nd
 * pass we would draw with black color and multiply by squished matrix, we should
 * get to this later with better approach
 *
 * so far we have been using immediate mode rendering, we should begin using
 * display lists / batch processing to reduce overhead, aka compiling commands
 *
 * it might be time to split the code, and make a shader version...
 *
 */

#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <sys/time.h>
#include "client.h"
#include "event.h"
#include "gldraw.h"
#include "glframe.h"
#include "gltools.h"
#include "math3d.h"
#include "platform.h"
#include "sdltools.h"
#include "window.h"

/* sane defaults, these are overwritable by config.lua */
int program_running = 1;
unsigned int xres_w = 640;
unsigned int yres_w = 480;
unsigned int bpp_w  = 32;
unsigned int af_w   = 8;
const char *window_caption   = "Textured Pyramid";
const char *window_icon_path = "tux.png";
const unsigned int sdl_video_flags = SDL_OPENGL | SDL_RESIZABLE;
unsigned int maxfps_w  = 60;

/* function prototypes */
static int client_init(struct client *);
static int client_destroy(struct client *);
static inline void process_events(void);
static void render(struct platform *);

static int client_init(struct client *p)
{
    p->textures[0] = gltLoadTGATexture("stone.tga");
    p->textures[1] = gltLoadTGATexture("grass.tga");

    /* display list, precompile commands */
    p->ground_list   = glGenLists(2);
    p->triangle_list = p->ground_list + 1;

    /* a triangle with a texture */
    glNewList(p->triangle_list, GL_COMPILE);
       glBindTexture(GL_TEXTURE_2D, p->textures[0]);
       /* glBindTexture(GL_TEXTURE_2D, p->textures[1]); */
       glColor3f(1.0f, 1.0f, 1.0f);
       glDrawTriangle();
    glEndList();

    /* a ground drawn using magneta lines */
    glNewList(p->ground_list, GL_COMPILE);
       glColor3ub(255, 0, 255);
       glDrawGround();
    glEndList();

    return 0;
}

static int client_destroy(struct client *p)
{
    glDeleteTextures(2, p->textures);
    glDeleteLists(p->ground_list, 2);

    return 0;
}

static inline void process_events(void)
{
    sdl_process_events();
}

static void render(struct platform *p)
{
    /* clear the window with current clearing color */
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    /* save the matrix state and do the rotations */
    glPushMatrix();
        /* apply camera transform, and draw the ground */
        glframe_apply_camera_transform(&p->camera, 1);
        glCallList(p->c->ground_list);

        glPushMatrix();
        /* move object back and do in place rotation */
        glTranslatef(0.0f, 0.2f, -3.5f);
        glRotatef(p->c->xrot, 1.0f, 0.0f, 0.0f);
        glRotatef(p->c->yrot, 0.0f, 1.0f, 0.0f);

        /* draw the pyramid */
        glCallList(p->c->triangle_list);
        glPopMatrix();

        /* draw a snowman */
        glTranslatef(0.0f, 0.0f, -7.0f);

        glDisable(GL_TEXTURE_2D);
        glDrawSnowman();
        glEnable(GL_TEXTURE_2D);

    /* restore the matrix state */
    glPopMatrix();

    /* buffer swap */
    SDL_GL_SwapBuffers();
}

int main(int argc, char **argv)
{
    struct platform p;
    memset(&p, 0, sizeof(p));

    /* setup client platform */
    struct client c;
    memset(&c, 0, sizeof(c));
    p.c = &c;
    p.client_init = &client_init;
    p.client_destroy = &client_destroy;

    /* initialize host platform */
    platform_init(&p);

    gltErrorCheck();
    gltOpenGLInfo();

    unsigned int startclock;
    while (program_running)
    {
        startclock = SDL_GetTicks();

        process_events();
        render(&p);

        sdlFrameControl(startclock);
    }

    platform_destroy(&p);
    puts("bye!");

    return 0;
}