-
Notifications
You must be signed in to change notification settings - Fork 2
Screen.h and .cpp
Hannupekka Sormunen edited this page Dec 10, 2018
·
12 revisions
//Header guards for SCREEN_H_
#ifndef SCREEN_H_
#define SCREEN_H
//Include C++ system libraries
#include <fstream>
#include <sstream>
#include <string>
//Include other libraries
#include <SDL.h>
//Create a namespace for this the use of this class
namespace particlefire {
//Create a class Screen inside namespace ParticleFire.
class Screen {
//Private data members of a Screen-class:
private:
SDL_Window *m_window;
SDL_Renderer *m_renderer;
SDL_Texture *m_texture;
Uint32 *m_buffer;
//Protected data members of a Screen-class:
protected:
static int screen_width_;
static int screen_height_;
static unsigned int target_fps_;
//Public data members of a Screen-class:
public:
//Constructors and destructor of a Screen-class:
public:
Screen();//Constructor for Screen-class
~Screen();//Destructor is used to deallocate processes and variables stored in to heap
//Private data methods of a Screen-class:
private:
bool ReadConfig();//Method for getting settings from a file
//Public data methods of a Screen-class:
public:
bool Init();//Method for initializing SDL-library. Returns true(1) if init was successful, else false(0)
void UpdateScreen();//Method for updating the window
void SetPixel(int x, int y, Uint8 red, Uint8 green, Uint8 blue);//Method for setting pixel value on the screen
void ApplyBoxBlur();//Method for blurring the particles
bool ProcessEvents();//Method for event processing
void LimitFPS(int elapsed);//Method for limiting frames per second(FPS)
};
} /* end of namespace ParticleFire */
#endif /* SCREEN_H */ Screen::Screen() :m_window(NULL), m_renderer(NULL), m_texture(NULL), m_buffer(NULL) {
//Constructor initializition list is used to set data members to NULL, otherwise SDL can't inform of failure or success when SDL instances are called.
//Call ReadConfig data method to get settings from the config.cfg-file
ReadConfig();
//Allocate pixel buffer arrays in to heap-memory so that they can be accessed throught the program
m_buffer = new Uint32[screen_width_*screen_height_];
}//Method for getting settings from a file
bool Screen::ReadConfig() {
//Access the file "config.cfg" and store it's position to variable file
std::ifstream file("config.cfg");
//Variable where to save each line of the file
std::string line;
//Variables for storing the values from file
int screen_width(0);
int screen_height(0);
int target_fps(0);
//Loop that goes throught the file line by line
while (std::getline(file, line)) {
std::istringstream sin(line.substr(line.find("=") + 1));
//If any of the following text snippets are found from the file these if-else statements are true and values are saved to variables
if (line.find("#") != -1) {
//do nothing, since lines starting with #-character are ignored
}
else if (line.find("SCREEN_WIDTH") != -1) {
sin >> screen_width;
}
else if (line.find("SCREEN_HEIGHT") != -1) {
sin >> screen_height;
}
else if (line.find("TARGET_FPS") != -1) {
sin >> target_fps;
}
}
//Check that the values from the file are greater than zero
if (screen_width > 0 && screen_height > 0 && target_fps > 0) {
//Save values to data members
screen_width_ = screen_width;
screen_height_ = screen_height;
target_fps_ = target_fps;
//Close the file "config.cfg"
file.close();
}
//If values are negative or zero file is closed and function returns false.
else {
//Close the file "config.cfg"
file.close();
return false;
}
return true;
}