Jump to content
  • Advertisement
Sign in to follow this  
etrek

SDL & OpenGL 2D not working

This topic is 3645 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am wanting to use SDL/OpenGL for a 2D/Sprite game. I have done a lot of googling and reading to figure out how to get a texture to a quad (seemingly simple from explanations). All I see is a window with the grey clear color though. If you know about SDL/OpenGL and textured quads, could you have a look at this code? Using VC2005, WindowsVista32 NOTE: This code is based on: http://www.libsdl.org/cgi/docwiki.cgi/OpenGL_Texture_Example Thanks... //The image is a wheel w/black background, just want round wheel part to show // its size is 256x256
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_opengl.h"

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define WIDTH 800
#define HEIGHT 600

GLuint textureid = 0;
int textw,texth;

void EnableTransparency()
{	
        glEnable(GL_TEXTURE_2D);
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}

GLuint LoadTexture(char *filename) {

        SDL_Surface *surface;
        GLuint textureid;
        int mode;

        surface = IMG_Load(filename);
        if (!surface) { return 0; }

        // work out what format to tell glTexImage2D to use...
        if (surface->format->BytesPerPixel == 3) {
                mode = GL_RGB
        } else if (surface->format->BytesPerPixel == 4) {
                mode = GL_RGBA;
        } else {
                SDL_FreeSurface(surface);
                return 0;
        }

        textw = surface->w;
        texth = surface->h;
   
        glGenTextures(1, &textureid);
        glBindTexture(GL_TEXTURE_2D, textureid);

        glTexImage2D(GL_TEXTURE_2D, 0, mode, surface->w, surface->h, 0, mode, GL_UNSIGNED_BYTE, surface->pixels);

        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

        SDL_FreeSurface(surface);
        return textureid;
}

void repaint() {
	float x = 200;
	float y = 200;

	glClear(GL_COLOR_BUFFER_BIT /*| GL_DEPTH_BUFFER_BIT*/);
	glLoadIdentity();		
        glBindTexture(GL_TEXTURE_2D, textureid);
        
	//glColor3ub(255, 0, 0);

        glBegin(GL_QUADS);
           // top left
           glTexCoord2i(0, 0);
           glVertex2f(x, y);

           // top right
           glTexCoord2i(1, 0);
           glVertex2f(x+textw, y);

           // bottom right
           glTexCoord2i(1, 1);
           glVertex2f(x+textw, y+texth);

           // bottom left
           glTexCoord2i(0, 1);
           glVertex2f(x, y+texth);
        glEnd();

        SDL_GL_SwapBuffers();
}

static void setup_sdl() 
{
    const SDL_VideoInfo* video;

    if ( SDL_Init(SDL_INIT_EVERYTHING) < 0 ) {
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
        exit(1);
    }

    atexit(SDL_Quit);

    video = SDL_GetVideoInfo( );

    if( !video ) {
        fprintf(stderr, "Couldn't get video info: %s\n", SDL_GetError());
        exit(1);
    }

    // Set the minimum requirements for the OpenGL window
    SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
    SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
    SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
    SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

    if( SDL_SetVideoMode( WIDTH, HEIGHT, video->vfmt->BitsPerPixel, SDL_OPENGL ) == 0 ) {
        fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
        exit(1);
    }

    EnableTransparency();
}

static void setup_opengl()
{
    float aspect = (float)WIDTH / (float)HEIGHT;
    glViewport(0, 0, WIDTH, HEIGHT);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    gluOrtho2D(0.0, WIDTH, 0.0, HEIGHT);
    //gluPerspective(60.0, aspect, 0.1, 100.0);
    glMatrixMode(GL_MODELVIEW);

    glClearColor(0.5, 0.5, 0.5, 0.0);
    
    glEnable(GL_DEPTH_TEST);

    glDisable(GL_CULL_FACE);
}

static void main_loop() 
{
    SDL_Event event;
    while(1) {
        while( SDL_PollEvent( &event ) ) {

            switch( event.type ) {
            case SDL_KEYDOWN:
                switch (event.key.keysym.sym) {
                case SDLK_ESCAPE:
                    exit(0);
                default:
                     break;
                }
			break;
            case SDL_QUIT:
               exit (0);
            }
        }
    // update the screen    
        repaint();
    }
}

int main(int argc,char **argv)
{
    setup_sdl();
    setup_opengl();
    textureid = LoadTexture("Media/ColWheel.tga");
    if(!textureid) return 1;
    main_loop();

    return 0;
}





[Edited by - etrek on June 25, 2008 6:33:43 PM]

Share this post


Link to post
Share on other sites
Advertisement
Enclosing your code in [source] tags will make it easier to read. You can edit your post using the 'edit' button in the upper right.

Share this post


Link to post
Share on other sites
If you just want to use OpenGL accelerated blitting of SDL surfaces there's already a glSDL(howto) backend for SDL. It seems to do all you want.
And you can take a look at how the rendering is implemented. That might help with your problem.

Share this post


Link to post
Share on other sites
Thanks for replys. :)
I was able to get a 2D/Sprite system working with the new NeHe tutorial SDL framework: http://nehe.gamedev.net/wiki/NewLessons.ashx

In the above code, the LoadTexture function works correctly, perhaps someone will find it useful.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!