Skip to main content
gamedev.net
🔒 Locked

help with.. stuff

Started by finky45 Jun 20, 2007 at 10:50 PM 4 replies 1.2k views
Original Post
finky45
finky45
DOH!!! This is my first graphic game, and first SDL game too. I know it's messy, I didn't mean to make it organized at all, just something real quick while i'm learning. please excuse the horrible format. Problem being, I can only see 1 projectile on the screen, and another one won't shoot until the last is off the screen. I would like to have the maximum (30) shot. I will work on the shooting timer later... I have 4 loops, one initializes the projectiles and is completed only once. -the 2nd checks if there any free (dead) projectiles when i press CTRL, and makes one active. it's supposed to break out of the loop in case 1 is made active, and move on to rendering 3rd will move the live projectiles. 4th checks if there are any live projectiles, and renders them. i think the problem is in here... what i am doing is blitting from a surface to many rectangles on the sc, that should work, right?

#include "SDL/SDL.h"
#include "SDL/SDL_ttf.h"
#include <string>
#include <sstream>

const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;

const int FPS_MAX = 30;

const int SHIP_SPEED = 10;
const int MAX_PROJ = 30;
const int BLASTER_SPEED = 5;

SDL_Surface* screenSurface = NULL;
SDL_Surface* shipSurface = NULL;
SDL_Surface* statsSurface = NULL;
SDL_Surface* blasterSurface = NULL;

SDL_Event event;

SDL_Rect screenRect;
SDL_Rect shipRect;
SDL_Rect statsRect;

TTF_Font* font = NULL;

SDL_Color fontColor = {255,243,170};

Uint32 backgroundColor;
Uint32 colorKey;

Uint8 *keystate = SDL_GetKeyState(NULL);

typedef struct{
    SDL_Rect rect;
    bool isDead;
} Projectile;

Projectile blaster[MAX_PROJ];

int main( int argc, char* argv[])
{
    
    int shipXvel=0;
    
    if( SDL_Init(SDL_INIT_VIDEO)== -1)
    {
        exit(1);
    }
    else
    {
        atexit(SDL_Quit);
    }
    
    if( TTF_Init() == -1 )
    {
        exit(1);
    }
    else
    {
        atexit(TTF_Quit);
    }
    
    screenSurface = SDL_SetVideoMode( SCREEN_WIDTH,SCREEN_HEIGHT,0,SDL_ANYFORMAT );
    if (screenSurface == NULL )
    {
        exit(1);
    }

    shipSurface = SDL_LoadBMP( "blueship.bmp" );
    if( shipSurface == NULL)
    {
        exit(1);
    }

    blasterSurface = SDL_LoadBMP( "blastershot.bmp" );
    if( blasterSurface == NULL)
    {
        exit(1);
    }

    font = TTF_OpenFont("ariblk.ttf", 16);
    if( font == NULL)
    {
        exit(1);
    }
    
    SDL_WM_SetCaption( "Space Shooter", NULL );

    screenRect.x = 0;
    screenRect.y = 0;
    screenRect.w = SCREEN_WIDTH;
    screenRect.h = SCREEN_HEIGHT;
    
    shipRect.x = SCREEN_WIDTH/2-12;
    shipRect.y = SCREEN_HEIGHT-75;
    shipRect.w = 25;
    shipRect.h = 25;

    statsRect.x = 0;
    statsRect.y = SCREEN_HEIGHT-20;
    statsRect.w = 0;
    statsRect.h = 0;

    for (int loop1=0; loop1 < MAX_PROJ; loop1++ )
    {
        blaster[loop1].rect.x=0;
        blaster[loop1].rect.y=0;
        blaster[loop1].rect.w=6;
        blaster[loop1].rect.h=12;
        blaster[loop1].isDead = true;
        fprintf(stdout, "loaded\n");
    }

    backgroundColor = SDL_MapRGB( screenSurface->format, 0, 0, 0 );
    
    colorKey = SDL_MapRGB( shipSurface->format, 255, 0, 255 );
    if( colorKey == NULL )
    {
        exit(1);
    }
    
    
    if( SDL_SetColorKey( shipSurface, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorKey) == -1)
    {
        exit(1);
    }

    if( SDL_SetColorKey( blasterSurface, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorKey) == -1)
    {
        exit(1);
    }    
    

    for(;;)
    {
        if( SDL_PollEvent( &event ) == 0 )
        {
            if( keystate[SDLK_LCTRL] )
            {
                for( int loop1 = 0; loop1 < MAX_PROJ; loop1++)
                {
        
                    if( blaster[loop1].isDead == true )
                    {
                         blaster[loop1].rect.x = shipRect.x+9;
                         blaster[loop1].rect.y = shipRect.y-9;
                         blaster[loop1].isDead = false;
                         fprintf(stdout, "shot once");
                         break;
                    }
                }
            }
            
            if( keystate[SDLK_LEFT])
            {
                fontColor.r = 255;
                fontColor.g = 0;
                fontColor.b = 0;
                shipXvel=SHIP_SPEED*-1;
            }
            else if( keystate[SDLK_RIGHT])
            {
                fontColor.r = 0;
                fontColor.g = 0;
                fontColor.b = 255;
                shipXvel=SHIP_SPEED;
            }
            else
            {
                fontColor.r = 255;
                fontColor.g = 243;
                fontColor.b = 170;
                shipXvel=0;
            }

            if( SDL_GetTicks()%(1000/FPS_MAX) == 0 )
            {
                shipRect.x+=shipXvel;
                if( shipRect.x+25 > SCREEN_WIDTH )
                {
                    shipRect.x = SCREEN_WIDTH-25;
                }
                if( shipRect.x < 0)
                {
                    shipRect.x = 0;
                }
                
                for (int loop1=0; loop1 < MAX_PROJ; loop1++)
                {
                    if( blaster[loop1].rect.y == 0)
                    {
                        blaster[loop1].isDead = true;
                    }
                    if( blaster[loop1].isDead == false )
                    {
                        blaster[loop1].rect.y-=BLASTER_SPEED;
                    }
                }
                
                statsSurface = TTF_RenderText_Solid( font, "yo momma is fat", fontColor );
                SDL_FillRect( screenSurface, &screenRect, backgroundColor );
                if( SDL_BlitSurface( shipSurface, NULL, screenSurface, &shipRect ) == -1)
                {
                    exit(1);
                }
                SDL_BlitSurface( statsSurface, NULL, screenSurface, &statsRect );
                SDL_FreeSurface( statsSurface );
                
                for( int loop1=0; loop1 < MAX_PROJ; loop1++)
                {
                     if( blaster[loop1].isDead == false)
                     {
                         if( SDL_BlitSurface( blasterSurface, NULL, screenSurface, &blaster[loop1].rect) == -1)
                         {
                             exit(1);
                         }
                     }
                }
                
                SDL_UpdateRect( screenSurface, 0,0,0,0);
            }

        }
        if (event.type == SDL_QUIT)
           exit(1);
    }   
    
    for (int loop1=0; loop1 < MAX_PROJ; loop1++ )
    {
        if( blaster[loop1].isDead == false)
            fprintf(stdout, "alive");
    }
    
    return 0;
}



finky45
finky45
no that was just to test something.

anyone have any ideas, or is there anything i can do to make it easier for people to help me?
Palidine
Palidine
Quote:
Original post by finky45
is there anything i can do to make it easier for people to help me?


run it in the debugger. set a breakpoint at the beginning and step through the code. What is happening? Is the code executing as you envision? Are the values of variables correct?

-me

finky45
finky45
Quote:
Original post by Palidine

run it in the debugger. set a breakpoint at the beginning and step through the code. What is happening? Is the code executing as you envision? Are the values of variables correct?

-me


i don't think the debugger works properly, it just runs the whole thing no matter what. (i set the thing to yes in the linker options, and rebuilt still no go.)
im using dev-c++

is it hard to make a console window? anyone got a link to a tutorial?


EDIT:

I figured it out... i was actually blitting all the projectiles in the same frame so they were all there, just on top of one another. found this out by increasing the speed by 1 for each different projectile, it caused a nice fan effect. thx for trying to help me for those who did.

PS. I'm still looking for a console window tutorial, it would make debugging a hell of a lot easier

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.