~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/sdl_ogl_proc_alias.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-09-20 22:44:35 UTC
  • mfrom: (4.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20130920224435-apuwj4fsl3fqv1a6
Tags: 1.5.6~20130920~6010666-1
* New snapshot release
* Update the list of supported architectures to the same as libv8
  (Closes: #723129)
* emlibtool has been removed from upstream.
* Fix warning syntax-error-in-dep5-copyright
* Refresh of the patches

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************
 
2
 *                                                                 *
 
3
 *                        Using SDL With OpenGL                    *
 
4
 *                                                                 *
 
5
 *                    Tutorial by Kyle Foley (sdw)                 *
 
6
 *                                                                 *
 
7
 * http://gpwiki.org/index.php/SDL:Tutorials:Using_SDL_with_OpenGL *
 
8
 *                                                                 *
 
9
 *******************************************************************/
 
10
 
 
11
/*
 
12
THIS WORK, INCLUDING THE SOURCE CODE, DOCUMENTATION
 
13
AND RELATED MEDIA AND DATA, IS PLACED INTO THE PUBLIC DOMAIN.
 
14
 
 
15
THE ORIGINAL AUTHOR IS KYLE FOLEY.
 
16
 
 
17
THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY
 
18
OF ANY KIND, NOT EVEN THE IMPLIED WARRANTY OF
 
19
MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE,
 
20
ASSUMES _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE
 
21
RESULTING FROM THE USE, MODIFICATION, OR
 
22
REDISTRIBUTION OF THIS SOFTWARE.
 
23
*/
 
24
 
 
25
#include "SDL/SDL.h"
 
26
#include "SDL/SDL_image.h"
 
27
#include "SDL/SDL_opengl.h"
 
28
 
 
29
#include <stdio.h>
 
30
#include <string.h>
 
31
 
 
32
void (*true_glGenTextures)(GLsizei, GLuint*) = NULL;
 
33
 
 
34
void glGenTextures(GLsizei n, GLuint *textures) {
 
35
  printf("num? %d\n", n);
 
36
  true_glGenTextures(n + 1, textures); // correct the error, ensures we are gone through
 
37
}
 
38
 
 
39
int main(int argc, char *argv[])
 
40
{
 
41
    SDL_Surface *screen;
 
42
 
 
43
    // Slightly different SDL initialization
 
44
    if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {
 
45
        printf("Unable to initialize SDL: %s\n", SDL_GetError());
 
46
        return 1;
 
47
    }
 
48
 
 
49
    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // *new*
 
50
 
 
51
    screen = SDL_SetVideoMode( 640, 480, 16, SDL_OPENGL ); // *changed*
 
52
    if ( !screen ) {
 
53
        printf("Unable to set video mode: %s\n", SDL_GetError());
 
54
        return 1;
 
55
    }
 
56
 
 
57
    // Set the OpenGL state after creating the context with SDL_SetVideoMode
 
58
 
 
59
    glClearColor( 0, 0, 0, 0 );
 
60
 
 
61
    glEnable( GL_TEXTURE_2D ); // Needed when we're using the fixed-function pipeline.
 
62
 
 
63
    glViewport( 0, 0, 640, 480 );
 
64
 
 
65
    glMatrixMode( GL_PROJECTION );
 
66
    glPushMatrix(); // just for testing
 
67
    glLoadIdentity();
 
68
 
 
69
    glOrtho( 0, 640, 480, 0, -1, 1 );
 
70
 
 
71
    glMatrixMode( GL_MODELVIEW );
 
72
    glLoadIdentity();
 
73
 
 
74
    // Load the OpenGL texture
 
75
 
 
76
    GLuint texture; // Texture object handle
 
77
    SDL_Surface *surface; // Gives us the information to make the texture
 
78
 
 
79
    if ( (surface = IMG_Load("screenshot.png")) ) {
 
80
 
 
81
        // Check that the image's width is a power of 2
 
82
        if ( (surface->w & (surface->w - 1)) != 0 ) {
 
83
            printf("warning: image.bmp's width is not a power of 2\n");
 
84
        }
 
85
 
 
86
        // Also check if the height is a power of 2
 
87
        if ( (surface->h & (surface->h - 1)) != 0 ) {
 
88
            printf("warning: image.bmp's height is not a power of 2\n");
 
89
        }
 
90
 
 
91
        true_glGenTextures = SDL_GL_GetProcAddress("glGenTextures");
 
92
 
 
93
        // Have OpenGL generate a texture object handle for us
 
94
        glGenTextures( 0, &texture );
 
95
 
 
96
        // Bind the texture object
 
97
        glBindTexture( GL_TEXTURE_2D, texture );
 
98
 
 
99
        // Set the texture's stretching properties
 
100
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
 
101
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
 
102
 
 
103
        //SDL_LockSurface(surface);
 
104
 
 
105
        // Add some greyness
 
106
        memset(surface->pixels, 0x66, surface->w*surface->h);
 
107
 
 
108
        // Edit the texture object's image data using the information SDL_Surface gives us
 
109
        glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0,
 
110
                      GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels );
 
111
 
 
112
        //SDL_UnlockSurface(surface);
 
113
    }
 
114
    else {
 
115
        printf("SDL could not load image.bmp: %s\n", SDL_GetError());
 
116
        SDL_Quit();
 
117
        return 1;
 
118
    }
 
119
 
 
120
    // Free the SDL_Surface only if it was successfully created
 
121
    if ( surface ) {
 
122
        SDL_FreeSurface( surface );
 
123
    }
 
124
 
 
125
    // Clear the screen before drawing
 
126
    glClear( GL_COLOR_BUFFER_BIT );
 
127
 
 
128
    // Bind the texture to which subsequent calls refer to
 
129
    glBindTexture( GL_TEXTURE_2D, texture );
 
130
 
 
131
    glBegin( GL_QUADS );
 
132
        glTexCoord2i( 0, 0 ); glVertex3f( 10, 10, 0 );
 
133
        glTexCoord2i( 1, 0 ); glVertex3f( 300, 10, 0 );
 
134
        glTexCoord2i( 1, 1 ); glVertex3f( 300, 128, 0 );
 
135
        glTexCoord2i( 0, 1 ); glVertex3f( 10, 128, 0 );
 
136
 
 
137
        glTexCoord2f( 0, 0.5 ); glVertex3f( 410, 10, 0 );
 
138
        glTexCoord2f( 1, 0.5 ); glVertex3f( 600, 10, 0 );
 
139
        glTexCoord2f( 1, 1   ); glVertex3f( 630, 200, 0 );
 
140
        glTexCoord2f( 0.5, 1 ); glVertex3f( 310, 250, 0 );
 
141
    glEnd();
 
142
 
 
143
    glBegin( GL_TRIANGLE_STRIP );
 
144
        glTexCoord2i( 0, 0 ); glVertex3f( 100, 300, 0 );
 
145
        glTexCoord2i( 1, 0 ); glVertex3f( 300, 300, 0 );
 
146
        glTexCoord2i( 1, 1 ); glVertex3f( 300, 400, 0 );
 
147
        glTexCoord2i( 0, 1 ); glVertex3f( 500, 410, 0 );
 
148
    glEnd();
 
149
 
 
150
    glDisable(GL_TEXTURE_2D);
 
151
 
 
152
    glColor3ub(90, 255, 255);
 
153
    glBegin( GL_QUADS );
 
154
        glVertex3f( 10, 410, 0 );
 
155
        glVertex3f( 300, 410, 0 );
 
156
        glVertex3f( 300, 480, 0 );
 
157
        glVertex3f( 10, 470, 0 );
 
158
    glEnd();
 
159
 
 
160
    glBegin( GL_QUADS );
 
161
        glColor3f(1.0, 0, 1.0);   glVertex3f( 410, 410, 0 );
 
162
        glColor3f(0, 1.0, 0);     glVertex3f( 600, 410, 0 );
 
163
        glColor3f(0, 0, 1.0);     glVertex3f( 600, 480, 0 );
 
164
        glColor3f(1.0, 1.0, 1.0); glVertex3f( 410, 470, 0 );
 
165
    glEnd();
 
166
 
 
167
    SDL_GL_SwapBuffers();
 
168
 
 
169
#if !EMSCRIPTEN
 
170
    // Wait for 3 seconds to give us a chance to see the image
 
171
    SDL_Delay(3000);
 
172
#endif
 
173
 
 
174
    // Now we can delete the OpenGL texture and close down SDL
 
175
    glDeleteTextures( 1, &texture );
 
176
 
 
177
    SDL_Quit();
 
178
 
 
179
    return 0;
 
180
}