Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Need some help with a class based sprite engine

This topic is 5161 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

Could a moderator close this thread please. I reposted with a more apt discription and a much shorter message. Thanks First off is this the right place to post for some C++ help? I am just learning c++. I am developing a sprite engine (learning as I go) and am stumped. I have been looking through these forums for some time (anon). This is the first time posting or getting help. So I don't know how to post the code except by massive cut and paste. I have a sprite class that creates an array of actions. The actions are a class as well. During my testing before i implemented the sprite class the action class worked fine. now that it is being called by the sprite class it no longer functions properly. The line "frames = new RECT[total_frames];" isn't creating an array of frames anymore it used to. frames is of type RECT. In debug mode total_frames is equal to 6. the rest of the code is as follows: if (frames == NULL){ //Place error handleing here } else { //build the Rect Array for texture positions SetupRects(top,left,height,width,texture_Height,texture_Width); } frames are equal to null now. so my SetupRects call is never made. This is happening in the CActions class in the initActions method. *note this is using the wonderful texture_quad batching example posted by Eamonn Doherty Code (not complete files): /************************************************************************************************* * MAIN.CPP *************************************************************************************************/ #include "main.h" //Entry point int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; IDirect3DTexture9* texture; //Start program execution bRunning = true; //Make a window and initialize DirectX in windowed mode MakeWindow(hInstance); InitD3D (0, 0, D3DFMT_A8R8G8B8, hWnd, true); //Load texture from file and setup destination rect texture = LoadTexture ("link1.png"); if (!texture) { MessageBox (hWnd, "Make sure texture.png is in working directory", "Error", MB_OK); return 0; } //new sprite //cppSprite mySprite; //CAction mySprite; //mySprite.initAction(0,0,32,32,6,"test",32,192,1,30,.4); CSprite mySprite("test", 0, 32, 32); //Setup source and destination rects //mySprite.SetupRects(); //Instaintiate the timer HiRes_Timer my_timer; //current frame of cow int curr_frame; curr_frame=0; //Main application loop while (bRunning) { my_timer.Start(); //QueryPerformanceCounter (&li_start); //Handle messages if (PeekMessage (&msg,NULL,0,0,PM_REMOVE)) { TranslateMessage (&msg); DispatchMessage (&msg); } //Clear buffer and draw sample graphic ClearBuffer (); BeginDrawing (); BeginBatchDrawing (texture); //Draw 16 quads //for (int quads = 0; quads < 3; quads++) // AddQuad(&rSource[quads], &rDest[quads], 0xFFFFFFFF); AddQuad(&mySprite.drawSprite() , &mySprite.screen_loc, 0xFFFFFFFF); //AddQuad(&mySprite.rSource[curr_frame], &mySprite.rDest[1], 0xFFFFFFFF); //AddQuad(&mySprite.rSource[curr_frame], &mySprite.rDest[2], 0xFFFFFFFF); //curr_frame++; //if (curr_frame > 5){ // curr_frame=0; //} EndBatchDrawing (); EndDrawing (); Present (); //Burn the time needed to make this x frames per second my_timer.Burn_Time(20); } //Cleaup loaded texture texture->Release (); //Close DirectX CloseD3D(); //Exit return 0; } //CSprite.h #include <d3d9.h> #include <d3dx9.h> #include "CAction.h" #ifndef CSPRITE_H #define CSPRITE_H class CSprite{ char *name; //Name of the Sprite int total_actions; //A count of the Sprites Actions int current_action; //The current Action the sprite is using int sprite_height; //The height of an individual sprite int sprite_width; //The width of an individual sprite CAction *actions; //pointer to array of actions public: CSprite(char *sprite_name, int aCount, int sHeight, int sWidth); ~CSprite(); RECT screen_loc; //the rect that holds the current screen location of the sprite void changeAction(int nAction); void move(int newX, int newY); char getSpriteName(); char getSpriteActionList(); RECT drawSprite(); }; #endif // CSPRITE_H ///:~ //CSprite.cpp #include <windows.h> #include "CSprite.h" //Constructor CSprite::CSprite(char *sprite_name, int aCount, int sHeight, int sWidth) { name = sprite_name; total_actions = aCount; current_action = 0; sprite_height = sHeight; sprite_width = sWidth; screen_loc.top = 0; screen_loc.bottom = 0 + sprite_height; screen_loc.left = 0; screen_loc.right = 0 + sprite_width; actions = new CAction[total_actions]; if (actions == NULL){ //Place error handleing here } else { //loop through the actions and initialize them actions[0].initAction(0,0,32,32,6,"test",32,192,1,30,.4); } } //Destructor CSprite::~CSprite() { delete[] actions; //remove the array from memory } void CSprite::changeAction(int nAction) { //check to see if a valid action has been passed, do a lookup of actions to get the id //set the new action current_action = nAction; } void CSprite::move(int newX, int newY) { //check to see if the values passed are proper //Set the new position of the sprite if valid screen_loc.top = newY; screen_loc.bottom = newY + sprite_height; screen_loc.left = newX; screen_loc.right = newX + sprite_width; } char CSprite::getSpriteName() { return *name; } char CSprite::getSpriteActionList() { //returns a list of actions for this sprite return *name; } RECT CSprite::drawSprite() { //returns the source and destination rectangles or there addresses return actions[current_action].GetCurrentFrame_RECT(); } //end CSprite.ccp //CAction.h #ifndef CACTION_H #define CACTION_H class CAction{ //private properties char *name; //The name of the action {Running, Walking, Dead....} int texture_id; //This needs to be changed to the address of the texture RECT *frames; //The rects that tell the texture where the animation is. This will be reset upon initialization int current_frame; //The current frame to be rendered int total_frames; //The count of animation frames in the State int frames_per_second; int total_frame_ticks; int current_frame_tick; double frames_per_tick; double seconds_per_animation; //private functions void changeFrames(); //Changes the current frame if needed by the ticks per frame SetupRects(int top, int left, int height, int width, int texture_Height, int texture_Width); RECT MakeRect(int left, int right, int top, int bottom); public: CAction(); ~CAction(); void initAction(int top, int left, int height, int width, int frame_count, char *action_name, int texture_Height, int texture_Width, int txtr_ID, int FPS, double SPA); RECT GetCurrentFrame_RECT(); //returns the address of the rect int getFrameCount(); int getCurrentFrame(); int getTextureID(); char getActionName(); void setFramesPerSecond(int FPS); int getFramesPerSecond(); }; #endif // CACTION_H ///:~ //CAction.cpp #include <windows.h> #include "CAction.h" //Constructor CAction::CAction(){ current_frame = 0; current_frame_tick = 0; } //Destructor CAction::~CAction(){ delete[] frames; //remove the array from memory } void CAction::changeFrames() { current_frame = (int)(current_frame_tick/frames_per_tick); current_frame_tick++; if (current_frame_tick >= total_frame_ticks) { current_frame_tick = 0; } if(current_frame >= total_frames) { current_frame = 0; } } void CAction::initAction(int top, int left, int height, int width, int frame_count, char *action_name, int texture_Height, int texture_Width, int txtr_ID, int FPS, double SPA) { total_frames = frame_count; current_frame = 0; name = action_name; texture_id = txtr_ID; frames_per_second = FPS; seconds_per_animation = SPA; total_frame_ticks = (int)(seconds_per_animation * frames_per_second); frames_per_tick = (double)total_frame_ticks / (double)total_frames; frames = new RECT[total_frames]; if (frames == NULL){ //Place error handleing here } else { //build the Rect Array for texture positions SetupRects(top,left,height,width,texture_Height,texture_Width); } } //Setup source and destination rectangles CAction::SetupRects (int top, int left, int height, int width, int texture_Height, int texture_Width) { int X; int Y; int curTop = top; int curLeft = left; int xTileWrap = 0; int yTileWrap = 0; for (int i = 0; i < total_frames; i++) { //Texture map for frame location X = (curLeft + (i * width)) - xTileWrap; //the current "LEFT" location Y = (curTop + (i * height)) + yTileWrap; //the current "TOP" location //check to see if we need to wrap either x or y if (X > texture_Width){ X = 0; xTileWrap += texture_Width; yTileWrap += height; } if (Y > texture_Height){ //an error occured the coordinates are off the texture } frames = MakeRect(X, X + width, Y, Y + height); } } //Fill a RECT structure RECT CAction::MakeRect(int left, int right, int top, int bottom) { RECT rTemp; rTemp.left = left; rTemp.right = right; rTemp.top = top; rTemp.bottom = bottom; return rTemp; } //return a RECTs address RECT CAction::GetCurrentFrame_RECT() { //RECT rTemp; // rTemp = frames[current_frame]; if (total_frames > 1) { changeFrames(); } return frames[current_frame]; } //return frame_count int CAction::getFrameCount() { return total_frames; } //return the current frame int CAction::getCurrentFrame() { return current_frame; } //return the texture id for this action int CAction::getTextureID() { return texture_id; } //return the name of this action char CAction::getActionName() { return *name; } void CAction::setFramesPerSecond(int FPS) { frames_per_second = FPS; } int CAction::getFramesPerSecond() { return frames_per_second; } [edited by - fencer on May 29, 2004 2:46:08 PM]

Share this post

Link to post
Share on other sites
As I'm only learning the basics of C++ myself, I cant help with your actual question, but yes, this is a good place to post for help.

code can be posted using [ code ] [ /code ] and [ source ] [ /source ] tags (without the spaces), which work like so:

This is some code posted in code tags, good for short pieces of code normally.

This is some code posted in source tags, these are better for larger chunks of code, and will perform some basic syntax highlighting.

[edited by - kazgoroth on May 29, 2004 11:44:46 AM]

Share this post

Link to post
Share on other sites

  • 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!