~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-06-11 15:45:24 UTC
  • mfrom: (1.2.1) (2.1.1 experimental)
  • Revision ID: package-import@ubuntu.com-20130611154524-rppb3w6tixlegv4n
Tags: 1.4.7~20130611~a1eb425-1
* New snapshot release
* Upload to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
        printf("Unable to set video mode: %s\n", SDL_GetError());
47
47
        return 1;
48
48
    }
49
 
    
 
49
 
50
50
    // Set the OpenGL state after creating the context with SDL_SetVideoMode
51
51
 
52
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
 
53
 
 
54
    glEnable( GL_TEXTURE_2D ); // Needed when we're using the fixed-function pipeline.
57
55
 
58
56
    glViewport( 0, 0, 640, 480 );
59
57
 
62
60
    glLoadIdentity();
63
61
 
64
62
    glOrtho( 0, 640, 480, 0, -1, 1 );
65
 
    
 
63
 
66
64
    glMatrixMode( GL_MODELVIEW );
67
65
    glLoadIdentity();
68
 
    
 
66
 
69
67
    // Load the OpenGL texture
70
68
 
71
69
    GLuint texture; // Texture object handle
72
70
    SDL_Surface *surface; // Gives us the information to make the texture
73
 
    
74
 
    if ( (surface = IMG_Load("screenshot.png")) ) { 
75
 
    
 
71
 
 
72
    if ( (surface = IMG_Load("screenshot.png")) ) {
 
73
 
76
74
        // Check that the image's width is a power of 2
77
75
        if ( (surface->w & (surface->w - 1)) != 0 ) {
78
76
            printf("warning: image.bmp's width is not a power of 2\n");
79
77
        }
80
 
    
 
78
 
81
79
        // Also check if the height is a power of 2
82
80
        if ( (surface->h & (surface->h - 1)) != 0 ) {
83
81
            printf("warning: image.bmp's height is not a power of 2\n");
84
82
        }
85
 
    
 
83
 
86
84
        // Have OpenGL generate a texture object handle for us
87
85
        glGenTextures( 1, &texture );
88
 
    
 
86
 
89
87
        // Bind the texture object
90
88
        glBindTexture( GL_TEXTURE_2D, texture );
91
 
        
 
89
 
92
90
        // Set the texture's stretching properties
93
91
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
94
92
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
99
97
        memset(surface->pixels, 0x66, surface->w*surface->h);
100
98
 
101
99
        // 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, 
 
100
        glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0,
103
101
                      GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels );
104
102
 
105
103
        //SDL_UnlockSurface(surface);
106
 
    } 
 
104
    }
107
105
    else {
108
106
        printf("SDL could not load image.bmp: %s\n", SDL_GetError());
109
107
        SDL_Quit();
110
108
        return 1;
111
 
    }    
112
 
    
 
109
    }
 
110
 
113
111
    // Free the SDL_Surface only if it was successfully created
114
 
    if ( surface ) { 
 
112
    if ( surface ) {
115
113
        SDL_FreeSurface( surface );
116
114
    }
117
 
    
 
115
 
118
116
    // Clear the screen before drawing
119
117
    glClear( GL_COLOR_BUFFER_BIT );
120
 
    
 
118
 
121
119
    // Bind the texture to which subsequent calls refer to
122
120
    glBindTexture( GL_TEXTURE_2D, texture );
123
121
 
140
138
        glTexCoord2i( 0, 1 ); glVertex3f( 500, 410, 0 );
141
139
    glEnd();
142
140
 
143
 
#if !EMSCRIPTEN
144
141
    glDisable(GL_TEXTURE_2D);
145
 
#endif
146
142
 
147
143
    glColor3ub(90, 255, 255);
148
144
    glBegin( GL_QUADS );
160
156
    glEnd();
161
157
 
162
158
    SDL_GL_SwapBuffers();
163
 
    
 
159
 
164
160
#if !EMSCRIPTEN
165
161
    // Wait for 3 seconds to give us a chance to see the image
166
162
    SDL_Delay(3000);
168
164
 
169
165
    // Now we can delete the OpenGL texture and close down SDL
170
166
    glDeleteTextures( 1, &texture );
171
 
    
 
167
 
172
168
    SDL_Quit();
173
 
    
 
169
 
174
170
    return 0;
175
171
}