Jump to content
  • Advertisement
Sign in to follow this  
dsage

collision/sprite drawing question

This topic is 4524 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 having trouble comming up with an idea of what to do about my situation. I'm playing with a top-down style game much like legend of zelda. At the moment i'm loading a text file with frame and collision information. I center the image on the bottom of the collision box and use the collision box as the x/y cords of the player. Sample collision/frame file. Note: name | # of frames | time in ms | frame rect in the bmp | collision box
name "Link"
animations 5
idle_down 1 1:{0,0,31,47}{0,0,26,26}
idle_up 1 1:{0,49,31,47}{0,0,26,26}
idle_right 1 1:{0,98,31,47}{0,0,26,26}
idle_left 1 1:{0,147,31,47}{0,0,26,26}
walk_down 9 100:{0,0,31,47}{0,0,26,26},100:{33,0,31,47}{0,0,26,26},100:{66,0,31,47}{0,0,26,26},100:{99,0,31,47}{0,0,26,26},100:{132,0,31,47}{0,0,26,26},100:{165,0,31,47}{0,0,26,26},100:{198,0,31,47}{0,0,26,26},100:{231,0,31,47}{0,0,26,26},100:{264,0,31,47}{0,0,26,26}



My question is this: Am I going about this in the right way? Should I have the frame centered in the center of the collision box? Should I use the collision box as the x/y cords? Also how does my animated sprite class look? frame.h
#ifndef FRAME_H
#define FRAME_H

#include <iostream>
#include "./sdl/sdl.h"

class CFrame
{
    public:
        CFrame();
        ~CFrame();
        int time;
        SDL_Rect rect;
        SDL_Rect coll;
};

#endif // FRAME_H




animation.h
#ifndef ANIMATION_H
#define ANIMATION_H

#include "frame.h"

class CAnimation
{
    public:
        CAnimation();
        ~CAnimation();
        std::string name;
        unsigned int count;
		signed int loopCount; // 0 dont loop / -1 loop forever
        CFrame *frames;
};

#endif // ANIMATION_H



anim_srpite.h
#ifndef ANIM_SPRITE_H
#define ANIM_SPRITE_H

#include <iostream>
#include <string>
#include "./sdl/sdl.h"
#include "map.h"
#include "animation.h"
#include "frame.h"

class CAnimSprite
{
	  public:
		  CAnimSprite();
		  ~CAnimSprite();
		  void render(SDL_Rect tempViewRect, SDL_Surface *dest);
		  void setcolorkey(const unsigned int tempR, const unsigned int tempG, const unsigned int tempB);
		  void setalpha(const unsigned int tempAlpha);
		  void setimage(SDL_Surface *tempImage);
		  void setpos(const float tempX, const float tempY);
		  void setvel(const float tempXVel, const float tempYVel);
		  void update();
		  void setinfo(const std::string &tempFileName);
		  void setanim(const int tempAnim, const signed int tempCount);
		  void setloop(const signed int tempCount);
		  void showcollsion(const bool tempBool);
		  std::string getname();
		  std::string getanimname();
		  float getx();
		  float gety();
		  int getw();
		  int geth();
		  int getcollx();
		  int getcolly();
		  int getcollw();
		  int getcollh();
		  int getloopcount();
		  int getanimation();
	  private:
		  std::string fileName;
		  std::string name;
		  SDL_Surface *image;
		  SDL_Rect s_rect;
		  SDL_Rect d_rect;
		  SDL_Rect coll_rect;
	  	  bool showCollision;
		  int r;
		  int g;
		  int b;
		  int alpha;
		  float x;
		  float y;
		  int w;
		  int h;
		  bool isLoaded;
		  int startTime;
		  int count;
		  int currentFrame;
		  int currentAnimation;
		  CAnimation *animations;
};

#endif // ANIM_SPRITE_H




And lastly how does my SDL_Surface manager look? I'm still very newbish using STL. man_image.cpp
#include "man_image.h"

CImageManager::CImageManager()
{
}

CImageManager::~CImageManager()
{
	for(std::map<std::string, SDL_Surface *>::iterator imageIter = imageMap.begin(); imageIter != imageMap.end(); ++imageIter)
	{
		if(imageIter->second != NULL)
		{
			SDL_FreeSurface(imageIter->second);
			std::cerr << "Freeing surface." << std::endl;
		}
	}
	
	imageMap.clear();
	std::cerr << "Clear() called on imageMap." << std::endl;
}

SDL_Surface * CImageManager::getimage(const std::string &tempFileName)
{
	std::map<std::string, SDL_Surface *>::iterator imageIter = imageMap.find(tempFileName);
	if(imageIter != imageMap.end())
	{
		std::cerr << "Found image: " << tempFileName << std::endl;
		return imageIter->second;
	}
	else
	{
		std::cerr << "Image not found. Trying to load: " << tempFileName << std::endl;
		
		SDL_Surface *image = SDL_LoadBMP(tempFileName.c_str());
		if(image == NULL)
		{
			std::cerr << "Couldn't find image: " << tempFileName << std::endl;
			return NULL;
		}

		imageMap[tempFileName] = image;
		
		std::map<std::string, SDL_Surface *>::iterator imageIter = imageMap.find(tempFileName.c_str());
		if(imageIter != imageMap.end())
		{
			std::cerr << "Loaded image: " << tempFileName << std::endl;
			return imageIter->second;
		}
		else
		{
			std::cerr << "Couldn't find image: " << tempFileName << std::endl;
			return NULL;
		}
	}
}




Share this post


Link to post
Share on other sites
Advertisement
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!