Browse Source

Formatted, added window settings

master
Macoy Madson 2 years ago
parent
commit
cc36498fce
2 changed files with 273 additions and 243 deletions
  1. +160
    -141
      graphics/graphics.cpp
  2. +113
    -102
      graphics/graphics.hpp

+ 160
- 141
graphics/graphics.cpp View File

@ -5,103 +5,116 @@
///////////////////////////////////////////////
// Window //////////////////////////////////
///////////////////////////////////////////////
window::window(int width,int height, std::string title)
{
isClearing=true;
void window::createWindow(int width, int height, std::string title)
{
sf::ContextSettings settings;
settings.depthBits = 24;
settings.stencilBits = 8;
settings.antialiasingLevel = 4;
// settings.majorVersion = 3;
// settings.minorVersion = 0;
isClearing = true;
win = new sf::RenderWindow(sf::VideoMode(width, height), title,
sf::Style::Close | sf::Style::Resize);
//win->setVerticalSyncEnabled(false);
//win->setFramerateLimit(45);
focused=true;
sf::Style::Close | sf::Style::Resize, settings);
// win->setVerticalSyncEnabled(false);
// win->setFramerateLimit(45);
}
window::window(int width, int height, std::string title)
{
createWindow(width, height, title);
focused = true;
time.start();
}
window::window(int width, int height, std::string title, WindowResizeCB onResizeCallback)
: onResize(onResizeCallback)
{
isClearing=true;
win = new sf::RenderWindow(sf::VideoMode(width, height), title,
sf::Style::Close | sf::Style::Resize);
//win->setVerticalSyncEnabled(false);
//win->setFramerateLimit(45);
focused=true;
time.start();
createWindow(width, height, title);
// focused = true;
// time.start();
}
//Close & delete the window
// Close & delete the window
window::~window()
{
win->close();
delete win;
win->close();
delete win;
}
void window::draw(sprite * spr)
void window::draw(sprite* spr)
{
if (spr) win->draw(spr->spr);
if (spr)
win->draw(spr->spr);
}
void window::draw(text *txt)
void window::draw(text* txt)
{
if (txt) win->draw(txt->str);
if (txt)
win->draw(txt->str);
}
void window::draw(pixels *pix)
void window::draw(pixels* pix)
{
if (pix) win->draw(pix->spr.spr);
if (pix)
win->draw(pix->spr.spr);
}
void window::update()
{
time.start();
win->display();
if (isClearing) win->clear(background);
win->display();
if (isClearing)
win->clear(background);
}
///Doesn't work
/// Doesn't work
float window::getFrameTime()
{
//return win->GetFrameTime(); //Looks like it's deprecated; let's just use a timer then
// return win->GetFrameTime(); //Looks like it's deprecated; let's just use a timer then
return time.getTime();
}
//This is a sloppy function because it does a lot more then tell whether or
//not the window should close, but it works well in this condition
// This is a sloppy function because it does a lot more then tell whether or
// not the window should close, but it works well in this condition
bool window::shouldClose()
{
sf::Event event;
while (win->pollEvent(event))
{
if (event.type==sf::Event::Closed) return true;
if (event.type==sf::Event::Resized)
{
sf::Event event;
while (win->pollEvent(event))
{
if (event.type == sf::Event::Closed)
return true;
if (event.type == sf::Event::Resized)
{
win->setView(
sf::View(sf::Vector2<float>(event.size.width / 2.f, event.size.height / 2.f),
sf::Vector2<float>(event.size.width, event.size.height)));
if (onResize)
onResize(event.size.width, event.size.height);
if (onResize)
onResize(event.size.width, event.size.height);
}
if (event.type==sf::Event::GainedFocus)
if (event.type == sf::Event::GainedFocus)
{
focused=true;
focused = true;
}
if (event.type==sf::Event::LostFocus)
if (event.type == sf::Event::LostFocus)
{
focused=false;
focused = false;
}
}
return false;
}
return false;
}
bool window::isFocused()
{
return focused;
return focused;
}
void window::setBackgroundColor(char r, char g, char b, char a)
{
background.r=r;
background.g=g;
background.b=b;
background.a=a;
background.r = r;
background.g = g;
background.b = b;
background.a = a;
}
//If you don't want to clear the window after updating (at all)
// If you don't want to clear the window after updating (at all)
void window::shouldClear(bool newState)
{
isClearing=newState;
isClearing = newState;
}
//Dimensions
// Dimensions
unsigned int window::getWidth()
{
return win->getSize().x;
@ -111,17 +124,18 @@ unsigned int window::getHeight()
return win->getSize().y;
}
//If you need the SFML window itself
// If you need the SFML window itself
sf::RenderWindow* window::getBase()
{
return win;
return win;
}
///////////////////////////////////////////////
// Text ///////////////////////////////////////
///////////////////////////////////////////////
bool text::loadFont(const char* fileName)
{
if (!font.loadFromFile(fileName)) return false;
if (!font.loadFromFile(fileName))
return false;
else
{
str.setFont(font);
@ -130,66 +144,67 @@ bool text::loadFont(const char* fileName)
}
void text::setText(std::string newText)
{
str.setString(newText);
str.setString(newText);
}
void text::setText(std::wstring newText)
{
str.setString(newText);
str.setString(newText);
}
void text::setPosition(float newX, float newY)
{
str.setPosition(newX, newY);
str.setPosition(newX, newY);
}
float text::getX()
{
return str.getPosition().x;
return str.getPosition().x;
}
float text::getY()
{
return str.getPosition().y;
return str.getPosition().y;
}
void text::setSize(unsigned int size)
{
str.setCharacterSize(size);
str.setCharacterSize(size);
}
void text::setColor(char r, char g, char b, char a)
{
color = sf::Color(r,g,b,a);
str.setColor(color);
color = sf::Color(r, g, b, a);
str.setColor(color);
}
void text::setAlpha(char a)
{
color.a = a;
str.setColor(color);
color.a = a;
str.setColor(color);
}
sf::Text* text::getBase()
{
return &str;
return &str;
}
///////////////////////////////////////////////
// Sprite /////////////////////////////////////
///////////////////////////////////////////////
bool sprite::load(const char* filename)
{
if (!srcImage.loadFromFile(filename)) return false;
spr.setTexture(srcImage);
return true;
if (!srcImage.loadFromFile(filename))
return false;
spr.setTexture(srcImage);
return true;
}
void sprite::setPosition(float newX, float newY)
{
spr.setPosition(newX, newY);
spr.setPosition(newX, newY);
}
void sprite::addVector(float newX, float newY)
{
spr.setPosition(newX+getX(), newY+getY());
spr.setPosition(newX + getX(), newY + getY());
}
float sprite::getX()
{
return spr.getPosition().x;
return spr.getPosition().x;
}
float sprite::getY()
{
return spr.getPosition().y;
return spr.getPosition().y;
}
unsigned int sprite::getWidth()
{
@ -201,19 +216,19 @@ unsigned int sprite::getHeight()
}
void sprite::setRotation(float rotation)
{
spr.setRotation(rotation);
spr.setRotation(rotation);
}
void sprite::setOrigin(float x, float y)
{
spr.setOrigin(x, y);
spr.setOrigin(x, y);
}
void sprite::setSmooth(bool useSmooth)
{
srcImage.setSmooth(useSmooth);
srcImage.setSmooth(useSmooth);
}
void sprite::setSubRect(int x, int y, int wx, int hy)
{
spr.setTextureRect(sf::Rect<int>(x,y,wx,hy));
spr.setTextureRect(sf::Rect<int>(x, y, wx, hy));
}
void sprite::clearSubRect()
{
@ -221,90 +236,94 @@ void sprite::clearSubRect()
}
sf::Sprite* sprite::getBase()
{
return &spr;
return &spr;
}
pixels::pixels(int newWidth, int newHeight)
{
width = newWidth;
height = newHeight;
rawPixels = new sf::Uint8[width * height * 4];
//Blank the pixels
for (int i = 0; i < height; ++i)
{
for (int n = 0; n < width; ++n)
{
int startIndex = (i * width * 4) + (n * 4);
rawPixels[startIndex] = 0;
rawPixels[startIndex + 1] = 0;
rawPixels[startIndex + 2] = 0;
rawPixels[startIndex + 3] = 0;
}
}
texture.create(width, height);
texture.update(rawPixels);
spr.getBase()->setTexture(texture);
}
pixels::pixels(int newWidth, int newHeight, unsigned char r, unsigned char g, unsigned char b, unsigned char a)
{
width = newWidth;
height = newHeight;
rawPixels = new sf::Uint8[width * height * 4];
//Blank the pixels
for (int i = 0; i < height; ++i)
{
for (int n = 0; n < width; ++n)
{
int startIndex = (i * width * 4) + (n * 4);
rawPixels[startIndex] = r;
rawPixels[startIndex + 1] = g;
rawPixels[startIndex + 2] = b;
rawPixels[startIndex + 3] = a;
}
}
texture.create(width, height);
texture.update(rawPixels);
spr.getBase()->setTexture(texture);
spr.setSmooth(false);
width = newWidth;
height = newHeight;
rawPixels = new sf::Uint8[width * height * 4];
// Blank the pixels
for (int i = 0; i < height; ++i)
{
for (int n = 0; n < width; ++n)
{
int startIndex = (i * width * 4) + (n * 4);
rawPixels[startIndex] = 0;
rawPixels[startIndex + 1] = 0;
rawPixels[startIndex + 2] = 0;
rawPixels[startIndex + 3] = 0;
}
}
texture.create(width, height);
texture.update(rawPixels);
spr.getBase()->setTexture(texture);
}
pixels::pixels(int newWidth, int newHeight, unsigned char r, unsigned char g, unsigned char b,
unsigned char a)
{
width = newWidth;
height = newHeight;
rawPixels = new sf::Uint8[width * height * 4];
// Blank the pixels
for (int i = 0; i < height; ++i)
{
for (int n = 0; n < width; ++n)
{
int startIndex = (i * width * 4) + (n * 4);
rawPixels[startIndex] = r;
rawPixels[startIndex + 1] = g;
rawPixels[startIndex + 2] = b;
rawPixels[startIndex + 3] = a;
}
}
texture.create(width, height);
texture.update(rawPixels);
spr.getBase()->setTexture(texture);
spr.setSmooth(false);
}
sprite* pixels::getSprite()
{
return &spr;
return &spr;
}
void pixels::setPixel(int x, int y, unsigned char r, unsigned char g, unsigned char b, unsigned char a)
void pixels::setPixel(int x, int y, unsigned char r, unsigned char g, unsigned char b,
unsigned char a)
{
if (x >= 0 && x < width && y >= 0 && y < height)
{
int startIndex = (y * width * 4) + (x * 4);
rawPixels[startIndex] = r;
rawPixels[startIndex + 1] = g;
rawPixels[startIndex + 2] = b;
rawPixels[startIndex + 3] = a;
}
if (x >= 0 && x < width && y >= 0 && y < height)
{
int startIndex = (y * width * 4) + (x * 4);
rawPixels[startIndex] = r;
rawPixels[startIndex + 1] = g;
rawPixels[startIndex + 2] = b;
rawPixels[startIndex + 3] = a;
}
}
void pixels::update()
{
texture.update(rawPixels);
texture.update(rawPixels);
}
bool pixels::getPixel(int x, int y, unsigned char& r, unsigned char& g, unsigned char& b, unsigned char& a)
bool pixels::getPixel(int x, int y, unsigned char& r, unsigned char& g, unsigned char& b,
unsigned char& a)
{
if (x >= 0 && x < width && y >= 0 && y < height)
{
int startIndex = (y * width * 4) + (x * 4);
r = rawPixels[startIndex];
g = rawPixels[startIndex + 1];
b = rawPixels[startIndex + 2];
a = rawPixels[startIndex + 3];
return true;
}
else return false;
if (x >= 0 && x < width && y >= 0 && y < height)
{
int startIndex = (y * width * 4) + (x * 4);
r = rawPixels[startIndex];
g = rawPixels[startIndex + 1];
b = rawPixels[startIndex + 2];
a = rawPixels[startIndex + 3];
return true;
}
else
return false;
}
int pixels::getWidth()
{
return width;
return width;
}
int pixels::getHeight()
{
return height;
return height;
}
#endif

+ 113
- 102
graphics/graphics.hpp View File

@ -1,29 +1,31 @@
/*This is the graphics layer for the game. It is meant to make it easy to switch out the graphics
* library at a later time, if necessary. It also makes it a little easier to handle scripting language
* bindings, if they are eventually added. Note that for now I won't be adding primitives like rectangles
* or lines. They will be added if needed.
* library at a later time, if necessary. It also makes it a little easier to handle scripting
* language bindings, if they are eventually added. Note that for now I won't be adding primitives
* like rectangles or lines. They will be added if needed.
* */
#ifndef GRAPHICS_HPP
#define GRAPHICS_HPP
#include <string>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include "../timer/timer.hpp" //Useful for getframetime
//To link:
//g++ -o "%e" "%f" -lsfml-graphics -lsfml-window -lsfml-system
#include <string>
#include "../timer/timer.hpp" //Useful for getframetime
// To link:
// g++ -o "%e" "%f" -lsfml-graphics -lsfml-window -lsfml-system
class window;
//Renderable text
// Renderable text
class text
{
friend class window;
friend class window;
private:
sf::Text str;
sf::Font font;
sf::Color color;
sf::Color color;
public:
void setText(std::string);
void setText(std::string);
void setText(std::wstring);
bool loadFont(const char*);
void setPosition(float newX, float newY);
@ -31,121 +33,130 @@ public:
float getY();
void setSize(unsigned int);
void setColor(char r, char g, char b, char a);
void setAlpha(char a);
//Returns the actual element used for rendering
void setAlpha(char a);
// Returns the actual element used for rendering
sf::Text* getBase();
};
//A single renderable image
// A single renderable image
class sprite
{
friend class window;
private:
friend class window;
private:
sf::Texture srcImage;
sf::Sprite spr;
public:
//Load and configure the sprite (false=failed)
bool load (const char* filename);
//Set the sprite's new position
// Load and configure the sprite (false=failed)
bool load(const char* filename);
// Set the sprite's new position
void setPosition(float, float);
//Add a vector-esque value to the position
// Add a vector-esque value to the position
void addVector(float, float);
//Get the sprite's current position
// Get the sprite's current position
float getX();
float getY();
//Rotate the sprite
// Rotate the sprite
void setRotation(float rotation);
//Change the origin
void setOrigin(float,float);
//Good for tilemap tiles
// Change the origin
void setOrigin(float, float);
// Good for tilemap tiles
void setSmooth(bool);
//Returns the width and height of the image
// Returns the width and height of the image
unsigned int getWidth();
unsigned int getHeight();
//Splits a part of the sprite out
//Takes the top left corner coordinates and a width and height (both bases)
void setSubRect(int,int,int,int);
//Back to whole sprite
// Splits a part of the sprite out
// Takes the top left corner coordinates and a width and height (both bases)
void setSubRect(int, int, int, int);
// Back to whole sprite
void clearSubRect();
//Return a direct pointer to the base sprite (lib dependant)
// Return a direct pointer to the base sprite (lib dependant)
sf::Sprite* getBase();
};
//An image with individually changeable pixels
// An image with individually changeable pixels
class pixels
{
friend class window;
private:
int width;
int height;
sf::Uint8* rawPixels;
sf::Texture texture;
sprite spr;
public:
//Creates the pixel array and sets the pixels to (0, 0, 0, 0);
//sets up the primary sprite as well. Note that the primary sprite
//smoothing is set to off by default
pixels(int newWidth, int newHeight);
//Creates the pixel array and sets the pixels to the provided color
//sets up the primary sprite as well. Note that the primary sprite
//smoothing is set to off by default
pixels(int newWidth, int newHeight, unsigned char r, unsigned char g, unsigned char b, unsigned char a);
//Use this function to manipulate the position etc. of the pixels
//through the master sprite
sprite* getSprite();
//Manipulate the color of a single pixel (RGBA); does bounds checking.
//Remember to call update() in order to see changes
void setPixel(int x, int y, unsigned char r, unsigned char g, unsigned char b, unsigned char a);
//Call after changing any pixels in order to see changes
void update();
//Sets the provided values to the color of the specified pixel; returns
//false if out of bounds
bool getPixel(int x, int y, unsigned char& r, unsigned char& g, unsigned char& b, unsigned char& a);
//Returns the width and height of the image
int getWidth();
int getHeight();
friend class window;
private:
int width;
int height;
sf::Uint8* rawPixels;
sf::Texture texture;
sprite spr;
public:
// Creates the pixel array and sets the pixels to (0, 0, 0, 0);
// sets up the primary sprite as well. Note that the primary sprite
// smoothing is set to off by default
pixels(int newWidth, int newHeight);
// Creates the pixel array and sets the pixels to the provided color
// sets up the primary sprite as well. Note that the primary sprite
// smoothing is set to off by default
pixels(int newWidth, int newHeight, unsigned char r, unsigned char g, unsigned char b,
unsigned char a);
// Use this function to manipulate the position etc. of the pixels
// through the master sprite
sprite* getSprite();
// Manipulate the color of a single pixel (RGBA); does bounds checking.
// Remember to call update() in order to see changes
void setPixel(int x, int y, unsigned char r, unsigned char g, unsigned char b, unsigned char a);
// Call after changing any pixels in order to see changes
void update();
// Sets the provided values to the color of the specified pixel; returns
// false if out of bounds
bool getPixel(int x, int y, unsigned char& r, unsigned char& g, unsigned char& b,
unsigned char& a);
// Returns the width and height of the image
int getWidth();
int getHeight();
};
typedef void (*WindowResizeCB)(float, float);
//The core of the graphics, the window.
// The core of the graphics, the window.
class window
{
private:
sf::RenderWindow *win;
sf::Color background;
bool isClearing;
bool focused;
timer time;
WindowResizeCB onResize;
public:
window(int width,int height, std::string title);
window(int width, int height, std::string title, WindowResizeCB onResizeCallback);
//Close & delete the window
~window();
//Various drawing functions
void draw(sprite * spr);
void draw(text *txt);
void draw(pixels *pix);
//Update the window
void update();
//Test if the window should close
bool shouldClose();
//If the window is focused (true) or buried (false)
bool isFocused();
//Time since last frame
float getFrameTime();
//Window configuration & management
void setBackgroundColor(char, char, char, char);
//If you don't want to clear the window after updating (at all)
void shouldClear(bool newState);
//Get the dimensions of the window
unsigned int getWidth();
unsigned int getHeight();
//Return a direct pointer to the base (library dependant)
sf::RenderWindow* getBase();
private:
sf::RenderWindow* win;
sf::Color background;
bool isClearing;
bool focused;
timer time;
WindowResizeCB onResize;
void createWindow(int width, int height, std::string title);
public:
window(int width, int height, std::string title);
window(int width, int height, std::string title, WindowResizeCB onResizeCallback);
// Close & delete the window
~window();
// Various drawing functions
void draw(sprite* spr);
void draw(text* txt);
void draw(pixels* pix);
// Update the window
void update();
// Test if the window should close
bool shouldClose();
// If the window is focused (true) or buried (false)
bool isFocused();
// Time since last frame
float getFrameTime();
// Window configuration & management
void setBackgroundColor(char, char, char, char);
// If you don't want to clear the window after updating (at all)
void shouldClear(bool newState);
// Get the dimensions of the window
unsigned int getWidth();
unsigned int getHeight();
// Return a direct pointer to the base (library dependant)
sf::RenderWindow* getBase();
};
#endif

Loading…
Cancel
Save