Skip to content

Commit

Permalink
AnimatedSprite added and first Shader tested
Browse files Browse the repository at this point in the history
  • Loading branch information
oj002 committed Nov 9, 2017
1 parent 902f55a commit 08fe706
Show file tree
Hide file tree
Showing 20 changed files with 202 additions and 143 deletions.
Binary file added SFML_Engine/Resources/res/player_0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SFML_Engine/Resources/res/player_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SFML_Engine/Resources/res/player_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 14 additions & 11 deletions SFML_Engine/Resources/shaders/Basic.shader
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
#shader vertex
#version 330 core

layout(location = 0) in vec4 position;

void main()
{
gl_Position = position;
}
// transform the vertex position
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

// transform the texture coordinates
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;

#shader fragment
#version 330 core
// forward the vertex color
gl_FrontColor = gl_Color;
}

layout(location = 0) out vec4 color;
#shader fragment
uniform sampler2D texture;
uniform float pixel_threshold;

void main()
{
color = vec4(0.2, 0.3, 0.8, 1.0);
}
float factor = 1.0 / (pixel_threshold + 0.001);
vec2 pos = floor(gl_TexCoord[0].xy * factor + 0.5) / factor;
gl_FragColor = texture2D(texture, pos) * gl_Color;
}
5 changes: 4 additions & 1 deletion SFML_Engine/SFML_Engine.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<ClCompile Include="SimpleSfmlEngine\core\Game.cpp" />
<ClCompile Include="SimpleSfmlEngine\core\InputManager.cpp" />
<ClCompile Include="SimpleSfmlEngine\core\StateMachine.cpp" />
<ClCompile Include="SimpleSfmlEngine\dataTypes\Animation.h" />
<ClCompile Include="SimpleSfmlEngine\core\AnimatedSprite.h" />
<ClCompile Include="SimpleSfmlEngine\dataTypes\clock.h" />
<ClCompile Include="SimpleSfmlEngine\dataTypes\matrix.h" />
<ClCompile Include="SimpleSfmlEngine\dataTypes\poolAllocator.h" />
Expand All @@ -57,6 +57,9 @@
<ClCompile Include="src\main.cpp" />
<ClCompile Include="src\States\SplashState.cpp" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\shaders\Basic.shader" />
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>15.0</VCProjectVersion>
<ProjectGuid>{75b4a9e2-edb1-4f44-ba40-5b11c24eb702}</ProjectGuid>
Expand Down
9 changes: 6 additions & 3 deletions SFML_Engine/SFML_Engine.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,6 @@
<ClCompile Include="SimpleSfmlEngine\core\AssetManager.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="SimpleSfmlEngine\dataTypes\Animation.h">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="SimpleSfmlEngine\dataTypes\clock.h">
<Filter>Source Files</Filter>
</ClCompile>
Expand All @@ -118,5 +115,11 @@
<ClCompile Include="SimpleSfmlEngine\dataTypes\stackAllocator.h">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="SimpleSfmlEngine\core\AnimatedSprite.h">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="Resources\shaders\Basic.shader" />
</ItemGroup>
</Project>
65 changes: 65 additions & 0 deletions SFML_Engine/SimpleSfmlEngine/core/AnimatedSprite.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#pragma once
#include <SFML/Graphics.hpp>
#include <string>
#include <map>
#include <vector>

namespace sse
{
class AnimatedSprite : public sf::Sprite
{
public:
struct Frame
{
Frame(sf::Texture& texture, float delay)
: m_texture(&texture)
, m_delay(delay)
{ }
sf::Texture* m_texture;
float m_delay;
};

std::vector<AnimatedSprite::Frame>& operator[](std::string s)
{
return m_animations[s];
}
//void AddAnimation(std::string name);

void SetActiveAnimation(std::string str)
{
m_currentAnimation = str;
}

void Update()
{
setTexture(*m_animations[m_currentAnimation][m_frameCounter].m_texture);
if (m_currentAnimation != "")
{
if (m_currentAnimation.find(m_currentAnimation) != m_currentAnimation.back())
{
if (m_clock.getElapsedTime().asSeconds() >= m_animations[m_currentAnimation][m_frameCounter].m_delay)
{

if (m_animations[m_currentAnimation].size() - 1 > m_frameCounter)
{
m_frameCounter++;
}
else
{
m_frameCounter = 0;
}

setTexture(*m_animations[m_currentAnimation][m_frameCounter].m_texture);
m_clock.restart();
}
}
}
}

private:
std::map<std::string, std::vector<AnimatedSprite::Frame> > m_animations;
size_t m_frameCounter = 0;
std::string m_currentAnimation = "";
sf::Clock m_clock;
};
}
1 change: 1 addition & 0 deletions SFML_Engine/SimpleSfmlEngine/core/AssetManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace sse
{

void AssetManager::LoadTexture(std::string name, std::string path)
{
sf::Texture tex;
Expand Down
1 change: 1 addition & 0 deletions SFML_Engine/SimpleSfmlEngine/core/AssetManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace sse

void LoadShaderFile(std::string name, std::string path);
sf::Shader &GetShader(std::string name);


private:
std::map<std::string, sf::Texture> m_textures;
Expand Down
31 changes: 6 additions & 25 deletions SFML_Engine/SimpleSfmlEngine/core/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,26 @@ namespace sse
Game::Game(unsigned int width, unsigned int height, std::string title, std::function<void(GameDataRef data)> runState)
{
this->m_data->window.create(sf::VideoMode(width, height), title, sf::Style::Close | sf::Style::Titlebar);

//this->m_data->machine.AddState(StateRef(new SplashState(this->m_data)));
this->m_data->window.setVerticalSyncEnabled(true);

runState(this->m_data);

this->Run();
}

void Game::Run()
{
float newTime, frameTime, interpolation;

float currentTime = this->m_clock.getElapsedTime().asSeconds();
float accumulator = 0.0f;

while (this->m_data->window.isOpen())
{
this->m_data->machine.ProcessStateChanges();
float dt = m_clock.restart().asSeconds();

newTime = this->m_clock.getElapsedTime().asSeconds();
frameTime = newTime - currentTime;

if (frameTime > 0.25f)
{
frameTime = 0.25f;
}

currentTime = newTime;
accumulator += frameTime;

while (accumulator >= dt)
{
this->m_data->machine.GetActiveState()->HandleInput();
this->m_data->machine.GetActiveState()->Update(dt);
this->m_data->machine.GetActiveState()->HandleInput();
this->m_data->machine.GetActiveState()->Update(dt);

accumulator -= dt;
}

interpolation = accumulator / dt;
this->m_data->machine.GetActiveState()->Render(interpolation);
this->m_data->machine.GetActiveState()->Render(dt);

}
}
Expand Down
1 change: 0 additions & 1 deletion SFML_Engine/SimpleSfmlEngine/core/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ namespace sse
Game(unsigned int width, unsigned int height, std::string title, std::function<void(GameDataRef data)> runState);

private:
const float dt = 1.0f / 60.0f;
sf::Clock m_clock;

GameDataRef m_data = std::make_shared<GameData>();
Expand Down
1 change: 0 additions & 1 deletion SFML_Engine/SimpleSfmlEngine/dataTypes.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once

#include "dataTypes\Animation.h"
#include "dataTypes\clock.h"
#include "dataTypes\matrix.h"
#include "dataTypes\poolAllocator.h"
Expand Down
47 changes: 0 additions & 47 deletions SFML_Engine/SimpleSfmlEngine/dataTypes/Animation.h

This file was deleted.

60 changes: 27 additions & 33 deletions SFML_Engine/SimpleSfmlEngine/dataTypes/matrix.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <iostream>
#include <vector>
#include <array>
#include <functional>
#include <string>

namespace sse
{
Expand All @@ -23,32 +23,29 @@ namespace sse
m_data[y * T_X + x] = value;
}

void print() const {
for (size_t y = 0; y < T_Y; y++) {
for (size_t x = 0; x < T_X; x++) {
std::cout << at(x, y) << " ";
}
std::cout << "\n";
}
}

size_t x() const { return T_X; }
size_t y() const { return T_Y; }

void SetPrintFunc(std::function<void(Matrix* m)> printFunc) { m_printFunc = printFunc; }
void print()
void print() const
{
std::cout << "Matrix(" << T_X << ", " << T_Y << ")[";
std::string s;
s += "Matrix(";
s += std::to_string(T_X) + ", ";
s += std::to_string(T_Y) + ")[";
for (size_t y = 0; y < T_Y; y++)
{
std::cout << "\ny = " << y << "[";
s += "\ny = ";
s += std::to_string(y);
s += "[";
for (size_t x = 0; x < T_X - 1; x++)
{
std::cout << at(x, y) << ", ";
s += std::to_string(at(x, y)) + ", ";
}
std::cout << at(T_X - 1, y) << "]";
s += std::to_string(at(T_X - 1, y)) + "]";
}
std::cout << "]" << std::endl;
s += "]\n";
std::puts(s.c_str());
fflush(stdout);
}

void loopThrough(std::function<inline void(size_t x, size_t y, T& val)> func)
Expand Down Expand Up @@ -79,32 +76,29 @@ namespace sse
m_data[y * T_X + x] = value;
}

void print() const {
for (size_t y = 0; y < T_Y; y++) {
for (size_t x = 0; x < T_X; x++) {
std::cout << at(x, y) << " ";
}
std::cout << "\n";
}
}

size_t x() const { return T_X; }
size_t y() const { return T_Y; }

void SetPrintFunc(std::function<void(Matrix* m)> printFunc) { m_printFunc = printFunc; }
void print()
void print() const
{
std::cout << "Matrix(" << T_X << ", " << T_Y << ")[";
std::string s;
s += "Matrix(";
s += std::to_string(T_X) + ", ";
s += std::to_string(T_Y) + ")[";
for (size_t y = 0; y < T_Y; y++)
{
std::cout << "\ny = " << y << "[";
s += "\ny = ";
s += std::to_string(y);
s += "[";
for (size_t x = 0; x < T_X - 1; x++)
{
std::cout << at(x, y) << ", ";
s += std::to_string(at(x, y)) + ", ";
}
std::cout << at(T_X - 1, y) << "]";
s += std::to_string(at(T_X - 1, y)) + "]";
}
std::cout << "]" << std::endl;
s += "]\n";
std::puts(s.c_str());
fflush(stdout);
}

void loopThrough(std::function<inline void(size_t x, size_t y, T& val)> func)
Expand Down
4 changes: 2 additions & 2 deletions SFML_Engine/SimpleSfmlEngine/dataTypes/poolAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace sse
{
if (m_openAllocations)
{
logger::logError<const char*>("In PoolAllocator: Not all allocations were successfully deallocated!");
logger::logError<const char*>("In PoolAllocator: Not all allocations were successfully deallocated!\n");
DEBUG_BREAK
}

Expand All @@ -78,7 +78,7 @@ namespace sse
{
if (m_head == nullptr)
{
logger::logError<const char*>("In PoolAllocator: PoolAllocator is full!");
logger::logError<const char*>("In PoolAllocator: PoolAllocator is full!\n");
DEBUG_BREAK
return nullptr;
}
Expand Down
Loading

0 comments on commit 08fe706

Please sign in to comment.