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

« back to all changes in this revision

Viewing changes to tests/sdl_ogl.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

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
int main(int argc, char *argv[])
 
33
{
 
34
    SDL_Surface *screen;
 
35
 
 
36
    // Slightly different SDL initialization
 
37
    if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {
 
38
        printf("Unable to initialize SDL: %s\n", SDL_GetError());
 
39
        return 1;
 
40
    }
 
41
 
 
42
    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // *new*
 
43
 
 
44
    screen = SDL_SetVideoMode( 640, 480, 16, SDL_OPENGL ); // *changed*
 
45
    if ( !screen ) {
 
46
        printf("Unable to set video mode: %s\n", SDL_GetError());
 
47
        return 1;
 
48
    }
 
49
    
 
50
    // Set the OpenGL state after creating the context with SDL_SetVideoMode
 
51
 
 
52
    glClearColor( 0, 0, 0, 0 );
 
53
    
 
54
#if !EMSCRIPTEN
 
55
    glEnable( GL_TEXTURE_2D ); // Need this to display a texture XXX unnecessary in OpenGL ES 2.0/WebGL
 
56
#endif
 
57
 
 
58
    glViewport( 0, 0, 640, 480 );
 
59
 
 
60
    glMatrixMode( GL_PROJECTION );
 
61
    glPushMatrix(); // just for testing
 
62
    glLoadIdentity();
 
63
 
 
64
    glOrtho( 0, 640, 480, 0, -1, 1 );
 
65
    
 
66
    glMatrixMode( GL_MODELVIEW );
 
67
    glLoadIdentity();
 
68
    
 
69
    // Load the OpenGL texture
 
70
 
 
71
    GLuint texture; // Texture object handle
 
72
    SDL_Surface *surface; // Gives us the information to make the texture
 
73
    
 
74
    if ( (surface = IMG_Load("screenshot.png")) ) { 
 
75
    
 
76
        // Check that the image's width is a power of 2
 
77
        if ( (surface->w & (surface->w - 1)) != 0 ) {
 
78
            printf("warning: image.bmp's width is not a power of 2\n");
 
79
        }
 
80
    
 
81
        // Also check if the height is a power of 2
 
82
        if ( (surface->h & (surface->h - 1)) != 0 ) {
 
83
            printf("warning: image.bmp's height is not a power of 2\n");
 
84
        }
 
85
    
 
86
        // Have OpenGL generate a texture object handle for us
 
87
        glGenTextures( 1, &texture );
 
88
    
 
89
        // Bind the texture object
 
90
        glBindTexture( GL_TEXTURE_2D, texture );
 
91
        
 
92
        // Set the texture's stretching properties
 
93
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
 
94
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
 
95
 
 
96
        //SDL_LockSurface(surface);
 
97
 
 
98
        // Add some greyness
 
99
        memset(surface->pixels, 0x66, surface->w*surface->h);
 
100
 
 
101
        // Edit the texture object's image data using the information SDL_Surface gives us
 
102
        glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0, 
 
103
                      GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels );
 
104
 
 
105
        //SDL_UnlockSurface(surface);
 
106
    } 
 
107
    else {
 
108
        printf("SDL could not load image.bmp: %s\n", SDL_GetError());
 
109
        SDL_Quit();
 
110
        return 1;
 
111
    }    
 
112
    
 
113
    // Free the SDL_Surface only if it was successfully created
 
114
    if ( surface ) { 
 
115
        SDL_FreeSurface( surface );
 
116
    }
 
117
    
 
118
    // Clear the screen before drawing
 
119
    glClear( GL_COLOR_BUFFER_BIT );
 
120
    
 
121
    // Bind the texture to which subsequent calls refer to
 
122
    glBindTexture( GL_TEXTURE_2D, texture );
 
123
 
 
124
    glBegin( GL_QUADS );
 
125
        glTexCoord2i( 0, 0 ); glVertex3f( 10, 10, 0 );
 
126
        glTexCoord2i( 1, 0 ); glVertex3f( 300, 10, 0 );
 
127
        glTexCoord2i( 1, 1 ); glVertex3f( 300, 128, 0 );
 
128
        glTexCoord2i( 0, 1 ); glVertex3f( 10, 128, 0 );
 
129
 
 
130
        glTexCoord2f( 0, 0.5 ); glVertex3f( 410, 10, 0 );
 
131
        glTexCoord2f( 1, 0.5 ); glVertex3f( 600, 10, 0 );
 
132
        glTexCoord2f( 1, 1   ); glVertex3f( 630, 200, 0 );
 
133
        glTexCoord2f( 0.5, 1 ); glVertex3f( 310, 250, 0 );
 
134
    glEnd();
 
135
 
 
136
    glBegin( GL_TRIANGLE_STRIP );
 
137
        glTexCoord2i( 0, 0 ); glVertex3f( 100, 300, 0 );
 
138
        glTexCoord2i( 1, 0 ); glVertex3f( 300, 300, 0 );
 
139
        glTexCoord2i( 1, 1 ); glVertex3f( 300, 400, 0 );
 
140
        glTexCoord2i( 0, 1 ); glVertex3f( 500, 410, 0 );
 
141
    glEnd();
 
142
 
 
143
#if !EMSCRIPTEN
 
144
    glDisable(GL_TEXTURE_2D);
 
145
#endif
 
146
 
 
147
    glColor3ub(90, 255, 255);
 
148
    glBegin( GL_QUADS );
 
149
        glVertex3f( 10, 410, 0 );
 
150
        glVertex3f( 300, 410, 0 );
 
151
        glVertex3f( 300, 480, 0 );
 
152
        glVertex3f( 10, 470, 0 );
 
153
    glEnd();
 
154
 
 
155
    glBegin( GL_QUADS );
 
156
        glColor3f(1.0, 0, 1.0);   glVertex3f( 410, 410, 0 );
 
157
        glColor3f(0, 1.0, 0);     glVertex3f( 600, 410, 0 );
 
158
        glColor3f(0, 0, 1.0);     glVertex3f( 600, 480, 0 );
 
159
        glColor3f(1.0, 1.0, 1.0); glVertex3f( 410, 470, 0 );
 
160
    glEnd();
 
161
 
 
162
    SDL_GL_SwapBuffers();
 
163
    
 
164
#if !EMSCRIPTEN
 
165
    // Wait for 3 seconds to give us a chance to see the image
 
166
    SDL_Delay(3000);
 
167
#endif
 
168
 
 
169
    // Now we can delete the OpenGL texture and close down SDL
 
170
    glDeleteTextures( 1, &texture );
 
171
    
 
172
    SDL_Quit();
 
173
    
 
174
    return 0;
 
175
}