Jump to content
  • Advertisement
Sign in to follow this  
Joshnathan

SDL_SetVideoMode() prob

This topic is 5043 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

Hi all, this is my problem: I have this screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE|SDL_DOUBLEBUF); which I thought would normally create a window of 640x480, but when I check colission like this:
keys = SDL_GetKeyState(NULL);
        if(keys[SDLK_UP]) 
        { 
            if(ypos > 0) { ypos -= 1; }
        }    
        if(keys[SDLK_DOWN]) 
        { 
            if(ypos < 480) { ypos += 1; }
        }    
        if(keys[SDLK_LEFT])
        {
            if(xpos > 0) { xpos -= 1; }
        }    
        if(keys[SDLK_RIGHT]) 
        { 
            if(xpos < 640) { xpos += 1; }
        } 


it is not right. The bottom of my screen(ypos max) is at 352(which I checked just by guessing). and the right border of my screen(xpos max) is also way less then 640. what am I doing wrong? P.S. In case of here is the FULL source:
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <SDL/SDL.h>

SDL_Surface *back;
SDL_Surface *image;
SDL_Surface *screen;
int xpos = 0, ypos = 0;

int InitImages()
{
    back = SDL_LoadBMP("bg.bmp");
    image = SDL_LoadBMP("image.bmp");
    return 0;
}    

int SDL_BlitSurface(SDL_Surface *scr, SDL_Rect *scrrect, SDL_Surface *dst, SDL_Rect *dstrect);

void DrawIMG(SDL_Surface *img, int x, int y)
{
    SDL_Rect dest;
    dest.x = x;
    dest.y = y;
    SDL_BlitSurface(img, NULL, screen, &dest);
}

void DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2)
{
    SDL_Rect dest;
    dest.x = x;
    dest.y = y;
    SDL_Rect dest2; 
    dest2.x = x2;
    dest2.y = y2;
    dest2.w = w;
    dest2.h = h;
    SDL_BlitSurface(img, &dest2, screen, &dest);
}

void DrawBG()
{
    DrawIMG(back, 0, 0);
}

void DrawScreen()
{
    DrawIMG(back, xpos-2, ypos-2, 132, 132, xpos-2, ypos-2);
    DrawIMG(image, xpos, ypos);
    SDL_Flip(screen);
}

int main(int argc, char *argv[])
{
    Uint8* keys;
    if (SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0)
    {
        std::cout<<"Unable to Initiate SDL: " << SDL_GetError();
        exit(1);
    }
    atexit(SDL_Quit);
    screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
    if (screen == NULL)
    {
        std::cout<< "Unable to set 640x480 video: " << SDL_GetError();
        exit(1);    
    }        
    InitImages();
    DrawBG();
    
    int done = 0;
    
    while(done == 0)
    {
        SDL_Event event;
        
        while(SDL_PollEvent(&event))
        {
            if(event.type == SDL_QUIT)
            {
                done = 1;
            }
            if(event.type == SDL_KEYDOWN)
            {
                if(event.key.keysym.sym == SDLK_ESCAPE)
                {
                    done = 1;
                }
            }
        }
        keys = SDL_GetKeyState(NULL);
        if(keys[SDLK_UP]) 
        { 
            if(ypos > 0) { ypos -= 1; }
        }    
        if(keys[SDLK_DOWN]) 
        { 
            if(ypos < 352) { ypos += 1; }
        }    
        if(keys[SDLK_LEFT])
        {
            if(xpos > 0) { xpos -= 1; }
        }    
        if(keys[SDLK_RIGHT]) 
        { 
            if(xpos < 640) { xpos += 1; }
        }    
        
        DrawScreen();
    }    
        
        return 0;
}  

Share this post


Link to post
Share on other sites
Advertisement
well.. for me it compiles, and I have the good resolution :|
And I'm not noticing any mistakes in your code.
Try to use "SDL.h" instead of <SDL/SDL.h>, I know that one is somewere used on the cone3d tutorials but I heared it;s better to use "sdl.h".

The image only leaves a trail behind, so try to use
SDL_FillRect(screen,NULL,SDL_MapRGB(screen->format,0,0,0);
everytime before the picture will be drawed.

Share this post


Link to post
Share on other sites
Because you want it to stop when it hits the edge, shouldn't it be:

keys = SDL_GetKeyState(NULL);
if(keys[SDLK_UP])
{
if(ypos > 0) { ypos -= 1; }
}
if(keys[SDLK_DOWN])
{
if(ypos + image->h < screen->h) { ypos += 1; }
}
if(keys[SDLK_LEFT])
{
if(xpos > 0) { xpos -= 1; }
}
if(keys[SDLK_RIGHT])
{
if(xpos + image->w < screen->w) { xpos += 1; }
}




[Edited by - Rob Loach on November 26, 2004 10:54:35 AM]

Share this post


Link to post
Share on other sites
I'm no expert - SDL has always given me exactly the window I asked for - but here's a guess:

You are _asking_ SDL to create a window with a particular size. You can use the SDL_Surface it returns to find out what the size really is (something like screen.w and screen.h - it really is that simple).

One reason it might fail is because you are asking for a surface with hardware support and your card doesn't support that resolution, although this seems unlikely if you are in windowed mode - GCards generally only start restricting you when you are in fullscreen mode.

You are also requesting a 32 bit surface - you could try SDL_ANYFORMAT and see if it helps (I use this, but then all my rendering is through OpenGL). What Graphics Card do you have?

Share this post


Link to post
Share on other sites
Quote:
Original post by Toadhead
The image only leaves a trail behind, so try to use
SDL_FillRect(screen,NULL,SDL_MapRGB(screen->format,0,0,0);
everytime before the picture will be drawed.

make that

SDL_FillRect(screen, 0, 0);

and save yourself a function call

I think Squirm is on to something. You really shouldn't be using those flags anyways without SDL_FULLSCREEN as well, since most graphics cards don't do hardware rendering in windows anyways.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!