Browse Source

Removed verbose CMake, fixed warnings, add polling

* No more cmake compile commands, because it's working now
* Release script will also explicitly set standard library etc.
* Added window::pollEventsUpdateState() for a slightly cleaner way to
  handle events I care about in other projects
* Fixed warnings related to setColor(). Added setOutlineColor().
* Reformatted some
master
Macoy Madson 1 year ago
parent
commit
cf04516646
4 changed files with 77 additions and 16 deletions
  1. +1
    -2
      BuildDependencies_Debug.sh
  2. +3
    -1
      BuildDependencies_Release.sh
  3. +57
    -7
      graphics/graphics.cpp
  4. +16
    -6
      graphics/graphics.hpp

+ 1
- 2
BuildDependencies_Debug.sh View File

@ -8,8 +8,7 @@ make clean
git clean -f
cmake . -DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_CXX_COMPILER=clang++ -DCMAKE_CXX_FLAGS="${CMAKE_CXX_FLAGS} -std=c++14 -stdlib=libstdc++" \
-DCMAKE_SHARED_LINKER_FLAGS="-stdlib=libstdc++" \
-DCMAKE_EXPORT_COMPILE_COMMANDS=TRUE -DCMAKE_VERBOSE_MAKEFILE=TRUE
-DCMAKE_SHARED_LINKER_FLAGS="-stdlib=libstdc++"
make -j4
cd ../../


+ 3
- 1
BuildDependencies_Release.sh View File

@ -6,7 +6,9 @@ echo "WARNING: This will run git clean -f on SFML"
cd dependencies/SFML
make clean
git clean -f
cmake .
cmake . -DCMAKE_BUILD_TYPE=Release \
-DCMAKE_CXX_COMPILER=clang++ -DCMAKE_CXX_FLAGS="${CMAKE_CXX_FLAGS} -std=c++14 -stdlib=libstdc++" \
-DCMAKE_SHARED_LINKER_FLAGS="-stdlib=libstdc++"
make -j4
cd ../../


+ 57
- 7
graphics/graphics.cpp View File

@ -1,5 +1,3 @@
#ifndef GRAPHICS_CPP
#define GRAPHICS_CPP
#include "graphics.hpp"
#include <iostream>
///////////////////////////////////////////////
@ -70,6 +68,40 @@ float window::getFrameTime()
// return win->GetFrameTime(); //Looks like it's deprecated; let's just use a timer then
return time.getTime();
}
// Returns whether the Closed event was received
bool window::pollEventsUpdateState(
std::function<void(sf::RenderWindow*, const sf::Event&)> eventCallback)
{
sf::Event event;
bool shouldClose = false;
while (win->pollEvent(event))
{
if (event.type == sf::Event::Closed)
shouldClose = true;
else 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);
}
else if (event.type == sf::Event::GainedFocus)
{
focused = true;
}
else if (event.type == sf::Event::LostFocus)
{
focused = false;
}
eventCallback(win, event);
}
return shouldClose;
}
// 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()
@ -98,11 +130,12 @@ bool window::shouldClose()
}
return false;
}
bool window::isFocused()
{
return focused;
}
void window::setBackgroundColor(char r, char g, char b, char a)
void window::setBackgroundColor(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
{
background.r = r;
background.g = g;
@ -142,44 +175,62 @@ bool text::loadFont(const char* fileName)
return true;
}
}
void text::setText(std::string newText)
{
str.setString(newText);
}
void text::setText(std::wstring newText)
{
str.setString(newText);
}
void text::setPosition(float newX, float newY)
{
str.setPosition(newX, newY);
}
float text::getX()
{
return str.getPosition().x;
}
float text::getY()
{
return str.getPosition().y;
}
void text::setSize(unsigned int size)
{
str.setCharacterSize(size);
}
void text::setColor(char r, char g, char b, char a)
void text::setColor(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
{
color = sf::Color(r, g, b, a);
str.setColor(color);
str.setFillColor(color);
}
void text::setOutlineColor(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
{
outlineColor = sf::Color(r, g, b, a);
str.setOutlineColor(outlineColor);
}
void text::setAlpha(char a)
{
color.a = a;
str.setColor(color);
outlineColor.a = a;
str.setFillColor(color);
str.setOutlineColor(outlineColor);
}
sf::Text* text::getBase()
{
return &str;
}
///////////////////////////////////////////////
// Sprite /////////////////////////////////////
///////////////////////////////////////////////
@ -326,4 +377,3 @@ int pixels::getHeight()
{
return height;
}
#endif

+ 16
- 6
graphics/graphics.hpp View File

@ -3,11 +3,12 @@
* 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
#pragma once
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <functional>
#include <string>
#include "../timer/timer.hpp" //Useful for getframetime
// To link:
@ -23,16 +24,22 @@ private:
sf::Text str;
sf::Font font;
sf::Color color;
sf::Color outlineColor;
public:
void setText(std::string);
void setText(std::wstring);
bool loadFont(const char*);
void setPosition(float newX, float newY);
float getX();
float getY();
void setSize(unsigned int);
void setColor(char r, char g, char b, char a);
void setColor(unsigned char r, unsigned char g, unsigned char b, unsigned char a);
void setOutlineColor(unsigned char r, unsigned char g, unsigned char b, unsigned char a);
void setAlpha(char a);
// Returns the actual element used for rendering
sf::Text* getBase();
@ -143,14 +150,18 @@ public:
void draw(pixels* pix);
// Update the window
void update();
// Test if the window should close
// Update events, including focused, and call callback for each event
// Returns whether the Closed event was received
bool pollEventsUpdateState(
std::function<void(sf::RenderWindow*, const sf::Event&)> eventCallback);
// Test if the window should close, and update events like focused
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);
void setBackgroundColor(unsigned char, unsigned char, unsigned char, unsigned char);
// If you don't want to clear the window after updating (at all)
void shouldClear(bool newState);
// Get the dimensions of the window
@ -159,4 +170,3 @@ public:
// Return a direct pointer to the base (library dependant)
sf::RenderWindow* getBase();
};
#endif

Loading…
Cancel
Save