Skip to content

Instantly share code, notes, and snippets.

@kenpower
Created January 26, 2024 13:48
Show Gist options
  • Save kenpower/d1b5e302556b31620f316012c52b0ab9 to your computer and use it in GitHub Desktop.
Save kenpower/d1b5e302556b31620f316012c52b0ab9 to your computer and use it in GitHub Desktop.
// Name:
// Login:
// Date:
// Approximate time taken:
//---------------------------------------------------------------------------
// Project description: TEMPLATE
// ---------------------------------------------------------------------------
// Known Bugs:
// ?
////////////////////////////////////////////////////////////
// include correct library file for release and debug versions
////////////////////////////////////////////////////////////
#ifdef _DEBUG
#pragma comment(lib,"sfml-graphics-d.lib")
#pragma comment(lib,"sfml-audio-d.lib")
#pragma comment(lib,"sfml-system-d.lib")
#pragma comment(lib,"sfml-window-d.lib")
#pragma comment(lib,"sfml-network-d.lib")
#else
#pragma comment(lib,"sfml-graphics.lib")
#pragma comment(lib,"sfml-audio.lib")
#pragma comment(lib,"sfml-system.lib")
#pragma comment(lib,"sfml-window.lib")
#pragma comment(lib,"sfml-network.lib")
#endif
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glu32.lib")
#include "Game.h" // include Game header file
int main()
{
Game aGame;
aGame.loadContent();
aGame.run();
return 0;
}
Game::Game() : window(sf::VideoMode(static_cast<int>(SCREEN_WIDTH), static_cast<int>(SCREEN_HEIGHT)), "Joint Project Game", sf::Style::Default)
// Default constructor
{
}
void Game::loadContent()
// Load the font file & setup the message string
{
if (!m_font.loadFromFile("ASSETS/FONTS/BebasNeue.otf"))
{
std::cout << "error with font file file";
}
// set up the message string
m_message.setFont(m_font); // set the font for the text
m_message.setCharacterSize(24); // set the text size
m_message.setFillColor(sf::Color::White); // set the text colour
m_message.setPosition(10, 10); // its position on the screen
//p.loadAssets("ASSETS\\face.png", sf::IntRect(4 * 146, 0 * 187, 146, 187));
playerRender.loadAssets("ASSETS\\face.png", sf::IntRect(4 * 146, 0 * 187, 146, 187));
}
void Game::run()
// This function contains the main game loop which controls the game.
{
srand((int)time(nullptr)); // set the seed once
sf::Time timePerFrame = sf::seconds(1.0f / 60.0f);
sf::Time timeSinceLastUpdate = sf::Time::Zero;
// the clock object keeps the time.
sf::Clock clock;
clock.restart();
while (window.isOpen())
{
// check if the close window button is clicked on
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
//get the time since last update and restart the clock
timeSinceLastUpdate += clock.restart();
//update every 60th of a second
//only when the time since last update is greater than 1/60 update the world.
if (timeSinceLastUpdate > timePerFrame)
{
update();
draw();
// reset the timeSinceLastUpdate to 0
timeSinceLastUpdate = sf::Time::Zero;
}
} // end while loop
}
void Game::update()
// This function takes the keyboard input and updates the game world
{
//p.update();
p.update(ui);
}
void Game::draw()
// This function draws the game world
{
// Clear the screen and draw your game sprites
window.clear();
m_message.setString("Use cursor keys to move");
window.draw(m_message); // write message to the screen
// p.render(window);
playerRender.render(window, p.posX(), p.posY());
window.display();
}
// Game class declaration
#pragma once
#include "SFML/Graphics.hpp"
#include <iostream>
#include <cstdlib> // include support for randomizing
#include <ctime> // supports ctime function
#include "Globals.h" // include Global header file
#include "Player.h" // include Player header file
class Game
{
sf::RenderWindow window;
Player p;
ImageRenderer playerRender;
UserInput ui;
public:
sf::Font m_font; // font for writing text
sf::Text m_message; // text to write on the screen
public: // declaration of member functions
Game(); // default constructor
void loadContent();
void run();
void update();
void draw();
};
#include<iostream>
#include "SFML/Graphics.hpp"
#include "Player.h" // include Player header file
//void Player::loadAssets(std::string imageFile, sf::IntRect clipRect ) {
void ImageRenderer::loadAssets(std::string imageFile, sf::IntRect clipRect ) {
if (!m_texture.loadFromFile(imageFile))
{
std::cout << "error with image file";
}
m_sprite.setTexture(m_texture);
m_sprite.setTextureRect(clipRect);
}
//void Player::render(sf::RenderWindow& window) {
void ImageRenderer::render(sf::RenderWindow& window, int x, int y) {
std::cout << "rendering at " << x << ", " << y << std::endl;
m_sprite.setPosition(x, y);
window.draw(m_sprite);
}
//void Player::update() {
// velX = velY = 0;
// if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
// {
// velX = -1;
// }
// if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
// {
// velY = -1;
// }
// if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
// {
// velX = +1;
// }
// if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
// {
// velY = +1;
// }
// x += velX;
// y += velY;
//
//}
Velocity UserInput::inputToVelocity() {
Velocity v{ 0,0 };
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
v.x = -1;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
v.y = -1;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
v.x = +1;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
v.y = +1;
}
return v;
}
void Player::update(UserInput& pc){
Velocity v = pc.inputToVelocity();
x += v.x;
y += v.y;
}
// Player class declaration
#pragma once
#include<string>
class ImageRenderer {
sf::Texture m_texture;
sf::Sprite m_sprite;
public:
void loadAssets(std::string, sf::IntRect);
void render(sf::RenderWindow&, int, int);
};
struct Velocity {
int x, y;
};
class UserInput {
public:
Velocity inputToVelocity();
};
class Player
{
int x, y;
int velX, velY; // do we need anymore?
public:
Player() {
x = 100, y = 100;
velX = velY = 0;
}
int posX() { return x; }
int posY() { return y; }
//void loadAssets(std::string, sf::IntRect);
//void update();
//void render(sf::RenderWindow&);
void update(UserInput& pc);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment