-
Notifications
You must be signed in to change notification settings - Fork 911
olc::PixelGameEngine
olc::rcode Construct(uint32_t screen_w, uint32_t screen_h, uint32_t pixel_w, uint32_t pixel_h)
Construct a PixelGameEngine window which is screen_w x screen_h olc::Pixels in size. Each olc::Pixel is defined a being pixel_w x pixel_h "screen pixels", i.e those native to your monitor. Therefore you can calculate the size of your window in "screen pixels" as being (screen_w * pixel_w) x (screen_h * pixel_h). This function will internally prepare the PixelGameEngine for starting. If it can't do this it will return olc::FAIL, otherwise olc::OK.
olc::rcode Start()
Starts the prepared PixelGameEngine. This will create the window, and create a context for internal rendering, then start the main game engine thread. This function will block until it returns. If the PixelGameEngine ran succesfully it will return olc::OK. If for any reason it could not start, for example inadequate resources, it will return olc::FAIL.
int main()
{
Example demo;
if (demo.Construct(256, 240, 4, 4))
demo.Start();
return 0;
}
On its own, the PixelGameEngine does nothing. It is a C++ class that you must inherit from and optionally override the following three members.
class Example : public olc::PixelGameEngine // Note olc::PixelGameEngine is inherited from
{
public:
Example()
{
sAppName = "Example"; // You can name your application here
}
public:
bool OnUserCreate() override
{
// Called once at the start, so create things here
return true;
}
bool OnUserUpdate(float fElapsedTime) override
{
// called once per frame
return true;
}
// Optional!
bool OnUserDestroy() override
{
// Called when window is closed
return true;
}
};
If the engine successfully starts, this function is called before the main game loop begins. It gives you the opportunity to load resources. You can still load resources later but this is a convenient place to do so. If you return false form this function, the PixelGameEngine will stop (and Start() will return), so its important to return true to keep the PixelGameEngine running. Note: this function is called from the thread that runs the game loop, so memory/context/thread sensitive operations performed here, persist into the main game loop. For example, if you need to interact with hardware, this would be an ideal place to establish communication with it.
This is the core PixelGameEngine function. It is called once per "frame" in your application. Here is where you would perform your game updates and drawing operations. the parameter fElapsedTime represents the time elapsed in seconds since the last time this function was called. This value can be used to accommodate fluctuations in resource availability and duration of this function. For example, consider moving a ball across the screen with a constant "apparent" speed. Simply moving the ball N pixels each frame will work, but will not look consistent. Since Distance (m) = Speed (m/s) * Time (s), we can deduce that the number of pixels required to move varies based on time.
When you wish to exit the PixelGameEngine (from perhaps an in-game menu), you can return false from this function, otherwise return true to keep the engine running.
This method is mostly optional, but is called when the user closes the PixelGameEngine window, giving you the opportunity to unload resources, disconnect from hardware and be an all round tidy programmer. You can veto the closing action by returning false. This will stop the PixelGameEngine from closing, otherwise, return true to allow the application to exit gracefully. Note: In many javidx9 videos you do not see this function being used. Closing the window will in turn exit the process, thus clearing up any trivially allocated memory automatically. However be cautious if you are doing more sophisticated things with memory or connected devices.