Sign in to follow this  

Problem setting video mode in SDL

This topic is 1513 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'm not sure if this should be here or in the graphics forum. I'm using Devc++, it's the first time I've used SDL and I'm trying to set it up to use some existing software drawing routines. The code below works but if I try to move the call to create the graphics window ( gfx_buffer* screen=graphics(640,480); ) from main to the do_stuff function the program exits at SDL_SetVideoMode in the graphics function. I don't have much experience with C++ so I don't know if this is just something stupid but I can't see what it could be.

 

#include "SDL/SDL.h"
 
struct gfx_buffer
{
    int wwidth;
    int height;
    int pitch;
    unsigned int* pixels;
    SDL_Surface* surface;
};
 
gfx_buffer* graphics(int wwidth,int height)
{
    
    gfx_buffer* buffer;
    buffer->surface=SDL_SetVideoMode( wwidth, height, 32, SDL_SWSURFACE );
    buffer->wwidth=wwidth;
    buffer->height=height;
    buffer->pitch=(int)buffer->surface->pitch >>2;
    buffer->pixels=(unsigned int*)buffer->surface->pixels;
    SDL_LockSurface(buffer->surface);
    return buffer;
}
 
void flip(gfx_buffer* buffer)
{
    if (buffer->surface!=0)
    {
        SDL_UnlockSurface(buffer->surface);
        SDL_Flip(buffer->surface );
        SDL_LockSurface(buffer->surface);
    }
}
 
gfx_buffer* create_gfx_buffer(int wwidth,int height)
{
    gfx_buffer* buffer=new gfx_buffer;
    buffer->wwidth=wwidth;
    buffer->height=height;
    buffer->pitch=wwidth;
    buffer->pixels=new unsigned int[wwidth*height];
    return buffer;
}
 
void set_pixel(gfx_buffer* buffer,int x,int y,unsigned int argb)
{
    if(x>=0 && x<buffer->wwidth && y>=0 && y<buffer->height)
        *(buffer->pixels+x+y*buffer->pitch)=argb;
}
 
unsigned int get_pixel(gfx_buffer* buffer,int x,int y)
{
    if(x>=0 && x<buffer->wwidth && y>=0 && y<buffer->height)
        return *(buffer->pixels+x+y*buffer->pitch);
}
 
void fill_rect(gfx_buffer* buffer,int x,int y,int w,int h,unsigned int argb)
{
    for(int yy=y;yy<y+h;yy++)
        for(int xx=x;xx<x+w;xx++)
            set_pixel(buffer,xx,yy,argb);
}
 
void draw_image(gfx_buffer* buffer,gfx_buffer* src_buffer,int x,int y)
{
    for(int yy=0;yy<src_buffer->height;yy++)
        for(int xx=0;xx<src_buffer->wwidth;xx++)
            set_pixel(buffer,xx+x,yy+y,get_pixel(src_buffer,xx,yy));
}
 
void grab_image(gfx_buffer* buffer,gfx_buffer* src_buffer,int x,int y)
{
    for(int yy=0;yy<src_buffer->height;yy++)
        for(int xx=0;xx<src_buffer->wwidth;xx++)
            set_pixel(src_buffer,xx,yy,get_pixel(buffer,xx+x,yy+y));
}
 
void clear_buffer(gfx_buffer* buffer,unsigned int argb=0)
{
    fill_rect(buffer,0,0,buffer->wwidth,buffer->height,argb);
}
 
void do_stuff(gfx_buffer* screen)
//void do_stuff(void)
{
    
    //gfx_buffer* screen=graphics(640,480);
    
 
    gfx_buffer* test=create_gfx_buffer(200,400);
    
    
    unsigned int* pixels=(unsigned int*)screen->surface->pixels;
    for(int a=0;a<10000;a++)
    {
        *(pixels+a)=0xff00ff;
    }
    fill_rect(screen,100,200,100,50,0xffffff);
    grab_image(screen,test,0,0);
    draw_image(screen,test,400,50);
    
    flip(screen);
    
    SDL_Delay( 2000 );
}
 
int main( int argc, char* args[] )
{
    SDL_Init( SDL_INIT_EVERYTHING );
 
    gfx_buffer* screen=graphics(640,480);
    do_stuff(screen);
    //do_stuff();
    
    SDL_Quit();
 
    return 0;
}

Share this post


Link to post
Share on other sites

In the graphics method you never allocate the memory for the buffer variable. You just write to an arbitrary memory allocation and afterwards, pretty much everything can happen.

SDL is setting up the window and surface then the pixel pointer in my struct points to the pixels of the SDL surface. It's at SDL_SetVideoMode that the program closes so it's before I've written anything to the buffer.

 

Glad I'm not crazy or stupid DekuTree64, well at least about this.

Share this post


Link to post
Share on other sites

This topic is 1513 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this