I'm trying to store all my textures in an array. but the images aren't getting returned it would seem here's my code
I was under the assumption that my loadTex and loadSpri functions would return and store their data in the array that i made, but it doesn't seem to be working that way because when i run the game they don't show up, just white space. My code probably looks bad, but i'm quite the beginner still and trying to get the hang of things
Here's render.cpp
#include "render.h" texandsprite render::textures[5]; sf::Texture loadTex(std::string); sf::Sprite loadSpri(sf::Texture); //Function to start the game void render::start() { _mainWindow.create(sf::VideoMode(1024, 768, 32), "A New Life"); textures[1].tex = loadTex("civilian.png"); textures[1].spri = loadSpri(textures[1].tex); GameLoop(); } void render::GameLoop() { sf::Event event; //Load(1, "civilian.png"); float x = 0; float y = 0; while(_mainWindow.isOpen()) { while (_mainWindow.pollEvent(event)) { //Check the type of event switch (event.type) { //window closed case sf::Event::Closed: _mainWindow.close(); break; case sf::Event::KeyPressed: switch(event.key.code) { case sf::Keyboard::Up: --y; break; case sf::Keyboard::Down: y += 5; break; case sf::Keyboard::Right: ++x; break; case sf::Keyboard::Left: --x; break; } break; default: break; } } _mainWindow.clear(sf::Color::White); //Draw everything here //_mainWindow.draw(...); textures[1].spri.setPosition(x,y); _mainWindow.draw(textures[1].spri); //End the current frame _mainWindow.display(); } } sf::RenderWindow render::_mainWindow; sf::Texture render::loadTex(std::string filename) { sf::Texture temptex; if(!temptex.loadFromFile(filename)) { std::cout << "Error: could not load image" << filename << std::endl; } std::cout << filename << " Loaded" << std::endl; return temptex; } sf::Sprite render::loadSpri(sf::Texture temptex) { sf::Sprite tempspri; tempspri.setTexture(temptex); return tempspri; }
Here's render.h
#ifndef RENDER_H_INCLUDED #define RENDER_H_INCLUDED #include "SFML/Window.hpp" #include "SFML/Graphics.hpp" #include <iostream> struct texandsprite { sf::Texture tex; sf::Sprite spri; }; class render { public: static void start(); //Here will go a list of all moving entities static texandsprite textures[5]; //Here will go a list of all nonmoving entities //static private: static void GameLoop(); static void Rendering(); static sf::Texture loadTex(std::string); static sf::Sprite loadSpri(sf::Texture); static sf::Texture texture; static sf::Sprite sprite; static sf::RenderWindow _mainWindow; }; #endif // RENDER_H_INCLUDED