Advertisement

Need help creating first game, using SDL

Started by January 24, 2003 12:30 PM
1 comment, last by MetroidHunter 21 years, 9 months ago
I've been working with SDL for about a week now. I'm trying to create a side scroller, similar to Mega Man. I've been having a bit of trouble getting the character to shoot and jump - when I press space bar (the button that is supposed to shoot) it starts to show the shooting animation, then the program crash. When I try to jump, it happens a little too fast, like in a split second he's back down again, and it leaves junk behind. - and I don't think I'm handling the animation efficiently. Could anyone help me out a bit? Here's my (sloppy) source code:// filename: main.cpp // author: Daniel Shupe // date: January 21, 2003 // // Game Type: Platformer/shooter // Game Title: ? // Controls: // left arrow - move ? left // right arrow - move ? right // space bar - fire blaster // J key - jump /* Soundtrack: [song (game / publisher) level used on] NOTE:not implemented yet Cut Man's Theme (Mega Man / Capcom) Factory level Bubble Man's Theme (Mega Man 2 / Capcom) Cruise Ship level Wood Man's Theme (Mega Man 2 / Capcom) Volcano level Spark Man's Theme (Mega Man 3 / Capcom) Space Fortress level The Last Palace (The Legend of Zelda II / Nintendo) Final Dungeon level */ /* Sprites Hero Sprites extracted from Mega Man */ /* NOTE: Character skin color is: Red 236 Green 211 Blue 74 Hue 34 Sat 194 Lum 146 */ #include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> // The SDL include files //#include "SDL_mixer.h" #include "SDL.h" // Sprite and Font Libraries //#include "CSpriteBase.h" //#include "CSprite.h" #include "font.h" #define MAX_ENERGY 100 #define BULLETS 5 SDL_Surface *back; SDL_Surface *image[2][15]; // surface array holds animations for ? SDL_Surface *screen; /* CSpriteBase bulletbase; // The Bullet sprite's base CSprite bullets[BULLETS]; // Individual bullets int bdraw[BULLETS]; // Shows which elements of the bullets // array are in use int bdirect[BULLETS]; // shows the direction of each bullet */ float sdlgt=0; // Stores the time that has passed float dt=0; // The time that has elapsed since // the previous frame int gameover=0; // Is the game over? int td=0,td2=0; // Used when checking the time difference // (how much time has passed since last frame) int xpos=100,ypos=390; int message = 0; int imgdirect = 1, imgframe = 0; // imgdirect = direction sprite is facing // imgframe = current animation being displayed int modify = 1; // used to change frames in animation int energy = MAX_ENERGY; // hero's energy is set to MAX_ENERGY // if energy becomes <= 0, game over int inair = 0; // determines if hero is airborne int airctr = 0; int InitImages() // this function fills sprite and background arrays with .bmp files { // background for level (will be changed) back = SDL_LoadBMP("factory.bmp"); // stand still, faceing left image[0][0] = SDL_LoadBMP("sprite000.bmp"); // animations for moving left image[0][1] = SDL_LoadBMP("sprite001.bmp"); image[0][2] = SDL_LoadBMP("sprite002.bmp"); image[0][3] = SDL_LoadBMP("sprite003.bmp"); image[0][4] = SDL_LoadBMP("sprite004.bmp"); image[0][5] = SDL_LoadBMP("sprite005.bmp"); image[0][6] = SDL_LoadBMP("sprite006.bmp"); // fire left, standing still image[0][7] = SDL_LoadBMP("sprite007.bmp"); // fire left while moving image[0][8] = SDL_LoadBMP("sprite008.bmp"); image[0][9] = SDL_LoadBMP("sprite009.bmp"); image[0][10] = SDL_LoadBMP("sprite010.bmp"); image[0][11] = SDL_LoadBMP("sprite011.bmp"); image[0][12] = SDL_LoadBMP("sprite012.bmp"); image[0][13] = SDL_LoadBMP("sprite013.bmp"); image[0][14] = SDL_LoadBMP("sprite014.bmp"); // stand still, faceing right image[1][0] = SDL_LoadBMP("sprite100.bmp"); // animations for moving right image[1][1] = SDL_LoadBMP("sprite101.bmp"); image[1][2] = SDL_LoadBMP("sprite102.bmp"); image[1][3] = SDL_LoadBMP("sprite103.bmp"); image[1][4] = SDL_LoadBMP("sprite104.bmp"); image[1][5] = SDL_LoadBMP("sprite105.bmp"); image[1][6] = SDL_LoadBMP("sprite106.bmp"); // fire right, standing still image[1][7] = SDL_LoadBMP(" sprite107.bmp"); // fire right, while moving image[1][8] = SDL_LoadBMP("sprite108.bmp"); image[1][9] = SDL_LoadBMP("sprite109.bmp"); image[1][10] = SDL_LoadBMP("sprite110.bmp"); image[1][11] = SDL_LoadBMP("sprite111.bmp"); image[1][12] = SDL_LoadBMP("sprite112.bmp"); image[1][13] = SDL_LoadBMP("sprite113.bmp"); image[1][14] = SDL_LoadBMP("sprite014.bmp"); return 0; } void DrawIMG(SDL_Surface *img, int x, int y) { SDL_Rect dest; dest.x = x; dest.y = y; SDL_SetColorKey(image[imgdirect][imgframe], SDL_SRCCOLORKEY, SDL_MapRGB(image[imgdirect][imgframe]->format, 0, 255, 0)); 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 DrawScene() { /*for(int i=0;i==1) {bullets.draw();} } */ DrawIMG(back, xpos-2, ypos-2, 66, 54, xpos-2, ypos-2); DrawIMG(image[imgdirect][imgframe], xpos, ypos); SDL_Flip(screen); } /* void addBullet() { // We loop through all the spots for bullets for(int i=0;i<BULLETS;i++) { // If we find an empty spot if(bdraw==0) { // Set the bullet's position if ( imgdirect = 0) {bullets.set(xpos-70,ypos+23); bdirect = 0;} if ( imgdirect = 1) {bullets.set(xpos+70,ypos+23); bdirect = 1;} // and mark the spot bdraw=1; return; } } } */ int main(int argc, char *argv[]) { Uint8* keys; if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 ) { printf("Unable to init SDL: %s\n", SDL_GetError()); exit(1); } atexit(SDL_Quit); /* if(Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 2048) < 0) { printf("Warning: Couldn't set 44100 Hz 16-bit audio\n- Reason: %s\n", SDL_GetError()); }*/ // Now we get ourself a 800x600x32 fullscreen surface… screen=SDL_SetVideoMode(800,600,32,SDL_SWSURFACE|SDL_FULLSCREEN); if ( screen == NULL ) { printf("Unable to set 800x600 video: %s\n", SDL_GetError()); exit(1); } InitImages(); DrawBG(); int done=0; while(done == 0) { SDL_Event event; /* td2=SDL_GetTicks(); dt=((float)(td2-td))*0.1; td=td2; */ 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(!inair) { if ( keys[SDLK_LEFT] ) { if(xpos > 0) xpos -= 1; imgdirect = 0; if (imgframe >= 6) modify = -1; // if at frame 5, reverse animation if (imgframe == 1) modify = 1; // if at frame 1, forward animation imgframe += modify; // change animation frame } if ( keys[SDLK_RIGHT] ) { if(xpos < 560) xpos += 1; imgdirect = 1; if (imgframe >= 6) modify = -1; // if at frame 6, reverse animation if (imgframe == 1) modify = 1; // if at frame 1, forward animation imgframe += modify; // change animation frame } if ( keys[SDLK_j] ) { ypos -= 30; imgframe = 13; inair = 1; airctr = 0; } if ( keys[SDLK_SPACE] ) { if (imgframe < 7) { imgframe += 7; } else { if (imgframe == 7) { modify = 0; } else if (imgframe == 13) { modify = -1; } else if (imgframe == 8) { modify = 1; } imgframe += modify; } //addBullet(); } if ( !keys[SDLK_SPACE] ) { if ( imgframe > 6 ) imgframe -= 7; } /* Loop through all the bullets for(int i=0;i<BULLETS;i++) { // If they are visible if(bdraw==1) { if (bdirect = 1) // then move them 3.5*dt units right bullets.xsub(3.5*dt); // If they are outside the screen area if(bullets.getx()>640) { // hide them bdraw=0; } } if(bdraw==1) { if (bdirect = 0) // then move them 3.5*dt units left bullets.xsub(3.5*dt); // If they are outside the screen area if(bullets.getx()<0) { // hide them bdraw=0; } } }*/ if ((!keys[SDLK_SPACE]) && (!keys[SDLK_LEFT]) && (!keys[SDLK_RIGHT]) && (!keys[SDLK_j])) { imgframe = 0; modify = 1;} } else { if (airctr == 2) { ypos += 45; airctr = 0; inair = 0; } if (airctr < 1) { airctr++; ypos -= 20; } else { airctr++; ypos += 25; } } DrawScene(); } return 0; } </i> <SPAN CLASS=editedby>[edited by - MetroidHunter &#111;n January 24, 2003 2:05:43 PM]</SPAN>
look how much frames in the shoot animation you have and if you have correctly space allocated in the animation other thing
its more easy that you have 1 surface whit all the frames of all animations and use the number of frames whit SDL_Rect so you can use only one surface whit all the data
Advertisement
i''ll have to look later but remember to use the [ source ] [ /source ] tags (without the spaces inbetween) when posting code.

Beginner in Game Development?  Read here. And read here.

 

This topic is closed to new replies.

Advertisement