| Ubuntu Tutorials  Windows Tutorials  Download  |
Add a 2D Sprite |
|
|
Written by: Andy Odle 1.) Download the Ball.zip file from here. 2.) Extract Ball.png and copy it into your game's "./debug/images/" folder. 3.) Open GOC.h in CodeLite. 4.) Add the includes for Sprite.h and MathGOC.h (See Code Example 1:).
Code Example 1:/* File Name: GOC.h */ /* Add new include files here */ #include "./source/PlatformSpecific/inc/Sprite.h" #include "./source/inc/MathGOC.h" 5.) Add a private Sprite pointer called ball_m to GOC.h (See Code Example 2:).
Code Example 2:/* File Name: GOC.h */ private: /* Add private variables here. */ Sprite *ball_m; }; #endif 6.) Save the changes to GOC.h. 7.) Open main.cpp in CodeLite.        Add the following code to the inside of GOC::Initialize() function.
A.) Create a PointGOC cordinate structure and name it ballPosition_l
B.) Initilize ballPosition_l's x-cordinate to half of the window's width, y-cordinate to
Example Code 3:
/*
File Name: main.cpp
*/
/*
Initialize your game scene, and game variables here.
*/
// Fill out the sprite's x-cor, y-cor and z-cor
// structure.
PointGOC ballPosition_l = {GetWindowWidth() / 2,
GetWindowHeight() / 2,
0.0f};
C.) Create a new Sprite instance for ball_m (See Example Code 4:). D.) Set ball_m's starting postion, width and height (See Example Code 4:). E.) Load and apply a texture to ball_m (See Example Code 4:). F.) Set the default visibility flag for ball_m (See Example Code 4:).
Code Example 4:
// Create a new instance of your sprite.
ball_m = new Sprite();
// Change the position of your sprite by passing
// in a filled out PointGOC structrue and the
// sprite's with and height.
ball_m->Set2DPosition(ballPosition_l,
24,
24);
// Apply a texture to your sprite.
ball_m->GetTexture()->LoadTexture("./images/Ball.png");
// Make your sprite visible or invisible.
ball_m->SetVisible(true);
}
       Add the following code to the inside of GOC::Destroy() function. A.) Delete the ball_m instance (See Example Code 5:).
Code Example 5:/* Clean up after your game here. */ // Destory your sprite when you are done with it. delete ball_m;
8.) Click Build > Build and Run Project if there were no errors you should see
Figure 1: First sprite on the screen.
2D Sprite Complete Example:       You can get the complete example source code here.
/*
File Name: GOC.h
Created By: Andy Odle
Date: July 27, 2009
*/
#ifndef GOC_h
#define GOC_h
#include "./source/PlatformSpecific/inc/ErrorReport.h"
#include "./source/inc/TimeGOC.h"
#include "./source/inc/InputEngine.h"
/* Add new include files here */
#include "./source/PlatformSpecific/inc/Sprite.h"
#include "./source/inc/MathGOC.h"
class GOC
{
public:
// Initilizes the developer's game.
virtual void Initialize();
// Updates the developer's game state.
virtual void Update(TimeGOC *timeGOC_p,
InputEngine *input_p);
virtual void IndepedentUpdate(TimeGOC *timeGOC_p,
InputEngine *input_p);
// Ceans up the developer's game.
virtual void Destroy();
// This is how you interact with the GOC.
//
// Output:
// Returns the Game Object Creator instance
// pointer.
static GOC *goc_i();
// Get the window's width and height.
int GetWindowWidth();
int GetWindowHeight();
// Start the setup of the Game Object Creator.
#ifdef _WIN32
void LoadGOC(HINSTANCE &hinstance_p);
#else
void LoadGOC();
#endif
// Clean up the Game Object Creator.
void UnloadGOC();
ErrorReport *GetErrorReport();
private:
GOC();
~GOC();
GOC(GOC const&){};
GOC &operator=(GOC const&){};
private:
/* Add private variables here. */
Sprite *ball_m;
};
#endif
/*
Date Created: Apr 29, 2010
FileName: main.cpp
First Sprite Example
*/
#include "./source/PlatformSpecific/inc/GOC.h"
void GOC::Initialize()
{
/*
Initialize your game scene, and game
variables here.
*/
// Fill out the sprite's x-cor, y-cor and z-cor
// structure.
PointGOC ballPosition_l = {GetWindowWidth() / 2,
GetWindowHeight() / 2,
0.0f};
// Create a new instance of your sprite.
ball_m = new Sprite();
// Change the position of your sprite by passing
// in a filled out PointGOC structrue and the
// sprite's with and height.
ball_m->Set2DPosition(ballPosition_l,
24,
24);
// Apply a texture to your sprite.
ball_m->GetTexture()->LoadTexture("./images/Ball.png");
// Make your sprite visible or invisible.
ball_m->SetVisible(true);
}
void GOC::Update(TimeGOC *timeGOC_p, InputEngine *input_p)
{
/*
Update your game scenes, check for collisions,
check for input and play sounds here.
*/
}
void GOC::IndepedentUpdate(TimeGOC* timeGOC_p, InputEngine* input_p)
{
/*
Update your game scenes, check for collisions,
check for input and play sounds here.
*/
}
void GOC::Destroy()
{
/*
Clean up after your game here.
*/
// Destory your sprite when you are done with it.
delete ball_m;
}
#ifdef _WIN32
// Define the windows main funciton.
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
#else
// Define the ubutnu main function.
int main(int argc, char **argv)
#endif
{
#ifdef _WIN32
// Start the GOC on windows.
GOC::goc_i()->LoadGOC(hInstance);
#else
// Start the GOC on ubuntu.
GOC::goc_i()->LoadGOC();
#endif
// Destroy and cleanup GOC.
GOC::goc_i()->UnloadGOC();
return 0;
}
Legal Info  About GOC  Contact Us  |
|