Advertisement

Blitting a PNG

Started by December 12, 2004 07:40 AM
5 comments, last by Yamian 20 years, 2 months ago
I'm trying to make a simple pong clone and making the paddle out of a png, but for some reason I see nothing blitted. I'm only trying the left one for now. Pleease don't get mad at my terrible procedure.

#include <stdlib.h>

#include <SDL\SDL_gfxPrimitives.h>
#include <SDL\SDL_Framerate.h>
#include <SDL\SDL_image.h>


void Slock(SDL_Surface *screen)
{
    if ( SDL_MUSTLOCK(screen) )
    {
        if ( SDL_LockSurface(screen) < 0 )
        {
            return;
        }
    }
}

void Sulock(SDL_Surface *screen)
{
    if ( SDL_MUSTLOCK(screen) )
    {
        SDL_UnlockSurface(screen);
    }
}

int main(int argc, char *argv[])
{
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Surface *screen = SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE);
    atexit(SDL_Quit);
    SDL_WM_SetCaption("Phlong",NULL);
    
    FPSmanager *framerate = new FPSmanager;
    SDL_initFramerate(framerate);
    SDL_setFramerate(framerate,100);
    
    SDL_Surface *temp;
    
    SDL_RWops *lrwop = SDL_RWFromFile("L_Paddle.png", "rb");
    SDL_Rect lrect;
    lrect.x=5;
    lrect.w=20;
    lrect.h=80; 
    
    int wait = 0;
    int ly=200,ry=200;
    float cx=320,cy=240;
    float cxa=2 ,cya=0;
    
    bool done = false;
    while(done == false)
    {
        SDL_Event event;
        
        while ( SDL_PollEvent(&event) )
        {
            if ( event.type == SDL_QUIT )  {  done = true;  }
            if ( event.type == SDL_KEYDOWN )
            {
                if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
            }
        }
        SDL_FillRect(screen,NULL,SDL_MapRGB(screen->format,0,0,0));
        Slock(screen);
        Uint8 *keys = SDL_GetKeyState(NULL);

            if     (keys[SDLK_LSHIFT])ly-=3;
            else if(keys[SDLK_LCTRL] )ly+=3;
            
            if     (keys[SDLK_UP]  )ry-=3;
            else if(keys[SDLK_DOWN])ry+=3;
            
            if(wait==150)wait=0;
            if(wait==0){cx+=cxa;cy+=cya;}
            else wait++;
            
            if((cx<=25)&&
               ((cy+10)>=ly)&&
               ((cy-10)<=ly+80)&&
               (cxa<0))
               {
                   cya=(cy-(ly+40))/10;
                   if(cya<0)cxa=4+cya;
                   else if(cya>0)cxa=4-cya;
                   else cxa=4;
               }
            if((cx<=25)&&
               ((ly+80)>480)&&
               ((cy-10)<=((ly+80)-480))&&
               (cxa<0))
               {
                   cya=(cy-((ly+40)-480))/10;
                   if(cya<0)cxa=4+cya;
                   else if(cya>0)cxa=4-cya;
                   else cxa=4;
               }
                
            
            if((cx>=640-25)&&
               ((cy+10)>=ry)&&
               ((cy-10)<=ry+80)&&
               (cxa>0))
               {
                   cya=(cy-(ry+40))/10;
                   if(cya<0)cxa=-4-cya;
                   else if(cya>0)cxa=-4+cya;
                   else cxa=-4;
               }
            if((cx>=(640-25))&&
               ((ry+80)>480)&&
               ((cy-10)<=((ry+80)-480))&&
               (cxa>0))
               {
                   cya=(cy-((ry+40)-480))/10;
                   if(cya<0)cxa=-4-cya;
                   else if(cya>0)cxa=-4+cya;
                   else cxa=-4;
               }
            
            if(((cy>=480)&&
               (cya>0))||
               ((cy<=0)&&
               (cya<0)))
                   cya=-cya;
            
            if((cx<0)||(cx>640)){wait=1;cx=320;cy=240;cxa=2;cya=0;}
            
            if(ly>480)ly=ly-480;
            if(ly<  0)ly=480-ly;
        
            if(ry>480)ry=ry-480;
            if(ry<  0)ry=480-ry;
            
            lrect.y=ly;
            
            rectangleRGBA(screen, 0, 0, 639, 479, 255, 255, 255, 255);
            
            //boxRGBA(screen,     5, ly,     25, ly+80, 255,255,255,255);
            boxRGBA(screen, 640-5, ry, 640-25, ry+80, 255,255,255,255);
            
            //if(ly+80>480)boxRGBA(screen,     5, 0,     25,((ly+80)-480),255,255,255,255);
            if(ry+80>480)boxRGBA(screen, 640-5, 0, 640-25,((ry+80)-480),255,255,255,255);
            
            if((wait>50)||(wait==0))filledCircleRGBA(screen,cx,cy,10,255,255,255,255);
        
        Sulock(screen);
        
        temp = IMG_LoadPNG_RW(lrwop);
        SDL_BlitSurface(screen,NULL,temp,&lrect);
        
        SDL_Flip(screen);
        SDL_framerateDelay(framerate);
    }
}    


Is there something special about the "rb" in rw from file?
I think it would be better for you to load your PNG like this:
#include <SDL.h>#include <SDL_image.h>int main(int argc, char *argv[]){  SDL_Surface *screen;  SDL_Surface *img;  SDL_Init(SDL_INIT_VIDEO);  screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_RESIZABLE);  if(!screen)    return 1;  /* That's all you have to do to load an image using SDL_image */  img = IMG_Load("file.png");  /* now you can play with your image like any other SDL_Surface */  /* But don't forget to SDL_FreeSurface it after you're finished */  SDL_FreeSurface(img);  SDL_Quit();  return 0;}


hope that helps !

If you want to take a look at SDL_image's documentation, go here

Matt
Matt
Advertisement
I'm getting a Undefined reference to it. I'm using dev-c++ so maybe some of the functions just don't work.
To solve the problem I found an older SDL_image devpak and somehow that mad it work =P. Oh well, thx for all.
Where was that older SDL_image devpak?
Rob Loach [Website] [Projects] [Contact]
I looked around and found this.

http://hno.freeshell.org

This topic is closed to new replies.

Advertisement