I have a method which should take a spritesheet which is 64x128 and turn it into 8x8 sprites.
after each texture is found from the stored Image, it is added to a sprite, and that sprite is added to a sprite array which is being called in my main method.
I have tested displaying a static sprite before, and my code worked (I displayed 1 8x8 sprite successfully)
However, when trying to display any of the 128 sprites in my list of sprites, now none are being rendered.
I believe the logic error may be in my MakeSprite method, but I'm unsure, and I can't see where the issue is.
[EDIT]: it seems that every sprite I call is returning the sprite at the very end of the spritesheet (sprite 128)
[Edit 2]: the texture (tex) of the previous sprite is overwritten by the next sprite to be spawned.
Below is a full verifiable working example of my code:
main.cpp
#include "Main.h"
int main() {
srand(time(NULL));
RenderWindow window(VideoMode(1280, 720), "Conduit");
MakeCircle(10, 100, 100, Color::White);
MakeCircle(30, 10, 100, Color::Cyan);
MakeCircle(100, 200, 100, Color::Magenta);
MakeCircle(100, 400, 100, Color::Cyan);
if (!LoadSpritesheet())
{
return 1;
}
while (window.isOpen()) {
Event event;
while (window.pollEvent(event)) {
if (event.type == Event::Closed)
window.close();
else if (event.key.code == Keyboard::Num1)
{
DrawRandomSprite(window);
}
}
window.clear();
DrawCircles(window);
int c = 0;
for (int i = 0; i < 128; i++)
{
Spritesheet.at(i).setPosition(c, c);
window.draw(Spritesheet.at(i));
c += 8;
}
window.display();
}
}
void DrawRandomSprite(RenderWindow &window)
{
//DEBUG METHOD: draws a random sprite for testing.
int sprite = rand() % 128 + 1;
Spritesheet.at(sprite).setPosition(rand() % 128 + 1, rand() % 128 + 1);
window.draw(Spritesheet.at(sprite));
}
void MakeCircle(float radius, float xpos, float ypos, Color color)
{
//makes a circle then adds it to the circle vector.
CircleShape shape;
shape.setRadius(radius);
shape.setPosition(xpos, ypos);
shape.setFillColor(color);
Circles.push_back(shape);
}
void DrawCircles(RenderWindow &window)
{
//Renders the circles in the circles vector.
for (int i = 0; i < Circles.size(); i++)
{
window.draw(Circles.at(i));
}
}
int LoadSpritesheet()
{
//make sure spritesheet exists, then loads it into an image.
Texture sheet;
if (!sheet.loadFromFile("Sprites/A.png"))
{
return 0;
}
else
{
sheetIMG = sheet.copyToImage();
SetMask();
MakeSprite(8, 4);
return 1;
}
}
void SetMask()
{
//creates a mask.
sheetIMG.createMaskFromColor(sf::Color(151, 56, 14, 0), 100);
}
void MakeSprite(int dimension, int scale)
{
//seperates the spritesheet into a list of 8x8 modular sprites.
int c = 0, r = 0;
do
{
for (int i = 0; i <= (sheetIMG.getSize().x * sheetIMG.getSize().y) / 64; i++)
{
if (r == 64)
break;
if (!tex.loadFromImage(sheetIMG, IntRect(c, r, dimension, dimension)))
break;
else
{
Sprite spr;
spr.setTexture(tex);
spr.setScale(scale, scale);
Spritesheet.push_back(spr);
c += dimension;
if (c == sheetIMG.getSize().x) { c = 0; r+=8; };
}
}
} while (r < sheetIMG.getSize().y);
}
Main.h
#pragma once
#include <SFML/Graphics.hpp>
#include <vector>
#include <stdio.h> /* printf, scanf, puts, NULL */
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
//standard and SFML namespaces;
using namespace std;
using namespace sf;
//===============================VARIBLES===========================
//the main spritesheet.
Texture tex;
Image sheetIMG;
//array to hold circles.
vector<CircleShape> Circles;
//array to hold sprites.
vector<Sprite> Spritesheet;
//===============================PROTOTYPES=============================
void DrawCircles(RenderWindow &window);
void MakeCircle(float radius, float xpos, float ypos, Color color);
void MakeSprite(int dimension, int scale);
void SetMask();
int LoadSpritesheet();
void DrawRandomSprite(RenderWindow &window);
There's two major issues with your code:
You're using global variables. While this might be okay with some libraries, with SFML this is not a good thing at all.
You're clearing the window after calling DrawRandomSprite. Therefore your sprite will never show up.
All your sprites have the same texture: tex. And as tex is a single global variable, when your loop finishes, it's set to the last texture. So all sprites will draw the last texture. You need to get rid of the globals and have one texture per sprite (or at least one per sprite that you want to have a different texture).
Related
I have to make Quarto game as a GUI program so firstly I wanted to make buttons in menu, but I don't know why it don't fill color when my mouse is at the button.
Code:
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include<bits/stdc++.h>
using namespace std;
using namespace sf;
class button{
public:
bool isClicked=false;
int Yposition;
int Xposition;
bool isMouseOn=false;
};
int main(){
//Defining variables
Vector2i mouseWindowPosition;
//rendering window "window"
RenderWindow window(VideoMode(800,600), "Quarto game - menu", Style::Default);
window.display();
//setting vsync
window.setVerticalSyncEnabled(true);
//loading textures
Texture backgroundTexture;
backgroundTexture.loadFromFile("Resources/background.png");
//making sprites
Sprite backgroundSprite;
backgroundSprite.setTexture(backgroundTexture);
backgroundSprite.setPosition(0,0);
//making buttons and their colors
RectangleShape playButton(Vector2f(200.f,50.f));
playButton.setFillColor(Color(128,128,128));
playButton.setOutlineThickness(5.f);
playButton.setOutlineColor(Color(100,100,100));
button play;
//setting position of buttons
play.Xposition=70;
play.Yposition=200;
//game loop
while(window.isOpen()){
Event event;
playButton.setFillColor(Color(128,128,128));
play.isMouseOn=false;
while(window.pollEvent(event)){
if(event.type==Event::Closed){
window.close();
}
}
//Getting mouse position
mouseWindowPosition=Mouse::getPosition(window);
if(mouseWindowPosition.x<=play.Xposition && mouseWindowPosition.y<=play.Yposition && mouseWindowPosition.x>=play.Xposition+200 && mouseWindowPosition.y>=play.Yposition+50){
play.isMouseOn=true;
}
//Drawing to screen
window.clear();
window.draw(backgroundSprite);
if(play.isClicked==false){
playButton.setPosition(Vector2f(play.Xposition, play.Yposition));
window.draw(playButton);
}
if(play.isMouseOn==true){
playButton.setFillColor(Color(128,128,128));
}
window.display();
}
}
Is there any better way to make buttons in sfml?
The immediate reason why the button doesn't fill is your if the statement has the signs reversed for checking the box. Using this if statement instead should work:
//Getting mouse position
mouseWindowPosition=Mouse::getPosition(window);
if (mouseWindowPosition.x>=play.Xposition && mouseWindowPosition.y>=play.Yposition &&
mouseWindowPosition.x<=play.Xposition+200 && mouseWindowPosition.y<=play.Yposition+50){
//your code here
}
If you are not already aware, the x and y values are arranged like a table where positive y goes down instead of a cartesian coordinate system where positive y is up.
The other problem is the color you are updating your fill with is the same color of the fill, so changing that color will get you your desired fill.
Also, logic wise you should swap the statement
if (play.isMouseOn == true)
{
}
with the statement
if (play.isClicked == false)
{
playButton.setPosition(Vector2f(play.Xposition, play.Yposition));
window.draw(playButton);
window.display();
}
Here is a working code that changes the fill from grey to red when you hover above it:
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <iostream>
using namespace std;
using namespace sf;
class button
{
public:
bool isClicked = false;
int Yposition;
int Xposition;
bool isMouseOn = false;
};
int main()
{
//Defining variables
Vector2i mouseWindowPosition;
//rendering window "window"
RenderWindow window(VideoMode(800, 600), "Quarto game - menu", Style::Default);
window.display();
//setting vsync
window.setVerticalSyncEnabled(true);
//loading textures
Texture backgroundTexture;
backgroundTexture.loadFromFile("Resources/background.png");
//making sprites
Sprite backgroundSprite;
backgroundSprite.setTexture(backgroundTexture);
backgroundSprite.setPosition(0, 0);
//making buttons and their colors
RectangleShape playButton(Vector2f(200.f, 50.f));
playButton.setFillColor(Color(100, 100, 100));
playButton.setOutlineThickness(5.f);
playButton.setOutlineColor(Color(100, 100, 100));
button play;
//setting position of buttons
play.Xposition = 70;
play.Yposition = 200;
//game loop
while (window.isOpen())
{
Event event;
playButton.setFillColor(Color(128, 128, 128));
play.isMouseOn = false;
while (window.pollEvent(event))
{
if (event.type == Event::Closed)
{
window.close();
}
}
//Getting mouse position
mouseWindowPosition = Mouse::getPosition(window);
if (mouseWindowPosition.x >= play.Xposition && mouseWindowPosition.y >= play.Yposition && mouseWindowPosition.x <= play.Xposition + 200 && mouseWindowPosition.y <= play.Yposition + 50)
{
play.isMouseOn = true;
}
//Drawing to screen
window.clear();
window.draw(backgroundSprite);
if (play.isMouseOn == true)
{
playButton.setFillColor(Color(128, 0, 0));
}
if (play.isClicked == false)
{
playButton.setPosition(Vector2f(play.Xposition, play.Yposition));
window.draw(playButton);
window.display();
}
}
}
I am working on a recreation of minesweeper using cpp and SFML. I have coded the board and the tiles switch to their revealed states when you left click on them, however, I am running into a strange issue with the right click.
Since I want to be able to flag and unflag tiles, I created a texture called tile_foreground that stores a texture the same size as the tiles but is completely transparent. The idea is that the first click will flag the tile and draw the flag sprite and a 2nd click would unflag the tile and change the sprite texture to the empty png so that only the base of the tile shows, with the cycle continuing on further clicks.
When you right click, the first click wont register and it will take at least a 2nd click to place down a flag on the tile. Sometimes it takes more than 2 clicks and clicking on a tile, moving your mouse to a different tile and then back to the same tile without clicking on a different tile will not flag the tile. It seems that the program doesnt register the first click as a click and waits for another one from the same tile to register it.
I suspect this comes from the way I handle the clicking events since before I added the statement below, holding down the right button would make the sprites alternate quickly and infinitely.
event.type == sf::Event::MouseButtonReleased
Any input would be appreciated since I dont really know how I would fix this.
Below is my main.cpp file
#include <SFML/Graphics.hpp>
#include <random>
#include <iostream>
#include <fstream>
#include <string>
#include "Board.h"
#include "TextureManager.h"
void testFunc() {
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(shape);
window.display();
}
}
void loadMSImages() {
string files[21] = {"debug", "digits", "face_happy", "face_lose", "face_win", "flag", "mine",
"number_1", "number_2", "number_3" , "number_4" , "number_5" , "number_6",
"number_7" , "number_8", "test_1", "test_2", "test_3", "tile_hidden", "tile_revealed", "tile_foreground"};
for (int i = 0; i < 21; i += 1) {
TextureManager::LoadTexture(files[i]);
}
}
int main()
{
loadMSImages();
//testFunc(); // Test function to see if program is still working properly
// Reads config file to determine window size
int colNum;
int rowNum;
int mineNum;
std::fstream reader("boards\\config.cfg");
string readString;
if (!reader.is_open())
{
std::cout << "File could not be opened" << std::endl;
return 0;
}
// Reads col #, row #, and mine # in that order
std::getline(reader, readString);
colNum = stoi(readString);
std::getline(reader, readString);
rowNum = stoi(readString);
std::getline(reader, readString);
mineNum = stoi(readString);
reader.close();
// Provided test boards have a size of 25 x 16
sf::RenderWindow window;
window.create(sf::VideoMode((colNum * 32), (rowNum * 32) + 100), "Minesweeper"); // Width, Height
window.setKeyRepeatEnabled(true);
window.setVerticalSyncEnabled(true);
Board testBoard(rowNum, colNum, mineNum);
testBoard.printBoard();
// Tile Testing Code
// Creating sprites for buttons on bottom of game
sf::Sprite testBoardOne;
sf::Sprite testBoardTwo;
sf::Sprite testBoardThree;
sf::Sprite debugButton;
sf::Sprite statusFace;
sf::Sprite timerDigitOne;
sf::Sprite timerDigitTwo;
sf::Sprite timerDigitThree;
// Setting the positions of these sprites
testBoardOne.setPosition(sf::Vector2f(((32 * colNum) - 64), ((32 * rowNum))));
testBoardTwo.setPosition(sf::Vector2f(((32 * colNum) -128), ((32 * rowNum))));
testBoardThree.setPosition(sf::Vector2f(((32 * colNum) - 192), ((32 * rowNum))));
debugButton.setPosition(sf::Vector2f(((32 * colNum) - 256), ((32 * rowNum))));
statusFace.setPosition(sf::Vector2f((((32 * colNum)/2)-32), ((32 * rowNum))));
// Loading the textures onto the sprites
testBoardOne.setTexture(TextureManager::GetTexture("test_1"));
testBoardTwo.setTexture(TextureManager::GetTexture("test_2"));
testBoardThree.setTexture(TextureManager::GetTexture("test_3"));
debugButton.setTexture(TextureManager::GetTexture("debug"));
statusFace.setTexture(TextureManager::GetTexture("face_happy"));
// 2D vector of tiles
std::vector<std::vector<Tile*>> tiles;
//Holding variables for creating Tile objects
Tile* holdingTile;
sf::Sprite* holdingSprite;
for (int i = 0; i < 25; i += 1)
{
std::vector<Tile*> holdingVec;
tiles.push_back(holdingVec);
for (int j = 0; j < 16; j += 1)
{
holdingTile = new Tile(0, i, j);
holdingSprite = new sf::Sprite();
holdingSprite->setTexture(TextureManager::GetTexture("tile_hidden"));
holdingSprite->setPosition(sf::Vector2f((32 * i), (32 * j)));
holdingTile->setBackgroundSprite(holdingSprite);
holdingSprite = new sf::Sprite();
//holdingSprite->setTexture(TextureManager::GetTexture("tile_foreground"));
holdingSprite->setPosition(sf::Vector2f((32 * i), (32 * j)));
holdingTile->setForegroundSprite(holdingSprite);
tiles.at(i).push_back(holdingTile);
}
}
//============================ GAME LOOP ======================================
// run the program as long as the window is open
while (window.isOpen())
{
// check all the window's events that were triggered since the last iteration of the loop
sf::Event event;
while (window.pollEvent(event))
{
// "close requested" event: we close the window
if (event.type == sf::Event::Closed)
window.close();
}
// Clears previous frame, draws new frame, and displays it
window.clear();
// Draws tiles on board
for (unsigned int i = 0; i < tiles.size(); i += 1)
{
for (unsigned int j = 0; j < tiles.at(i).size(); j += 1)
{
window.draw(*(tiles.at(i).at(j)->getBackgroundSprite()));
window.draw(*(tiles.at(i).at(j)->getForegroundSprite()));
}
}
//Draws "Constant" Buttons, such as debug mode and boards
window.draw(testBoardOne);
window.draw(testBoardTwo);
window.draw(testBoardThree);
window.draw(debugButton);
window.draw(statusFace);
window.display();
// Clicking handlers
if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
// transform the mouse position from window coordinates to world coordinates
sf::Vector2f mouse = window.mapPixelToCoords(sf::Mouse::getPosition(window));
// retrieve the bounding box of the sprite
sf::FloatRect bounds;
for (unsigned int i = 0; i < tiles.size(); i += 1)
{
for (unsigned int j = 0; j < tiles.at(i).size(); j += 1)
{
bounds = tiles.at(i).at(j)->getBackgroundSprite()->getGlobalBounds();
if (bounds.contains(mouse))
{
//std::cout << "Sprite pressed" << std::endl;
tiles.at(i).at(j)->getBackgroundSprite()->setTexture(TextureManager::GetTexture("tile_revealed"));
}
}
}
}
//if (sf::Mouse::isButtonPressed(sf::Mouse::Right)) // WIP - Rapid oscillation of textures
if (sf::Mouse::isButtonPressed(sf::Mouse::Right) && event.type == sf::Event::MouseButtonReleased)
{
// transform the mouse position from window coordinates to world coordinates
sf::Vector2f mouse = window.mapPixelToCoords(sf::Mouse::getPosition(window));
// retrieve the bounding box of the sprite
sf::FloatRect bounds;
for (unsigned int i = 0; i < tiles.size(); i += 1)
{
for (unsigned int j = 0; j < tiles.at(i).size(); j += 1)
{
bounds = tiles.at(i).at(j)->getBackgroundSprite()->getGlobalBounds();
if (bounds.contains(mouse))
{
std::cout << "Tile right clicked" << std::endl;
if (tiles.at(i).at(j)->isFlagged)
{
tiles.at(i).at(j)->unflagTile();
tiles.at(i).at(j)->getForegroundSprite()->setTexture(TextureManager::GetTexture("tile_foreground"));
}
else
{
tiles.at(i).at(j)->flagTile();
tiles.at(i).at(j)->getForegroundSprite()->setTexture(TextureManager::GetTexture("flag"));
}
}
}
}
}
}
// Make sure to clear the textures at the end of the program to avoid errors
TextureManager::Clear();
return 0;
}
The problem is that you use the sf::Event event variable outside the while (window.pollEvent(event)) loop, so when you use the event, the last event type is assigned to it, which does not necessarily have to be MouseButtonReleased but can be one of these:
https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Event.php#af41fa9ed45c02449030699f671331d4a
Solution:
Move this condition:
if (sf::Mouse::isButtonPressed(sf::Mouse::Right) && event.type == sf::Event::MouseButtonReleased)
to the while (window.pollEvent(event)) loop and change it to:
if (event.type == sf::Event::MouseButtonReleased && event.mouseButton.button == sf::Mouse::Right)
I'm making a Pacman game using SFML. I have a background image for the maze that I'm using and I was wondering how I would be able to use multiple bounding boxes for the background so that when the yellow Pacman character collides with the walls, it stops him. Also, I was wondering if I could use multiple bounding boxes for the background maze image, if so, how?
My code:
#include <SFML/Graphics.hpp>
#include <iostream>
int main(int argc, char** argv)
{
sf::RenderWindow renderWindow(sf::VideoMode(280, 480), "Pacman" /*, sf::Style::Titlebar | sf::Style::Close*/);
sf::Event event;
sf::Texture texture, texture2;
texture.setSmooth (true);
texture2.setSmooth (true);
texture.loadFromFile("images/pacmanbkgd.png");
sf::Sprite background_sprite(texture);
background_sprite.setScale(sf::Vector2f(1.25, 1.25));
texture2.loadFromFile("images/transparentPacman3Sprites.png");
sf::IntRect rectSourceSprite(0, 0, 18, 16);
sf::Sprite pacmanMC_sprite(texture2, rectSourceSprite); // sf::intrect(int rectleft, int recttop, int rectwidth, int rectheight)
// pacmanMC_sprite.setScale(sf::Vector2f(1, 1));
pacmanMC_sprite.move(sf::Vector2f(200, 345)); // set sprite at inital location point (140, 145)
sf::Clock clock;
//sets the origin of the sprite
sf::Rect<float> size = pacmanMC_sprite.getGlobalBounds();
pacmanMC_sprite.setOrigin(sf::Vector2f(size.width / 2, size.height / 2));
while (renderWindow.isOpen())
{
while (renderWindow.pollEvent(event))
{
if (event.type == sf::Event::EventType::Closed)
renderWindow.close();
}
if (clock.getElapsedTime().asSeconds() > 0.75f) // 0.07, used for making the mouth faster
{
if (rectSourceSprite.left == 34)
rectSourceSprite.left = 0;
else
rectSourceSprite.left += 17; // pushes the sprite towards the left using x axis
pacmanMC_sprite.setTextureRect(rectSourceSprite);
clock.restart();
}
// pacmanMC_sprite movement
// sprite keeps on moving to a different position when a the correct key is pressed for its rotation
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Up))
{
pacmanMC_sprite.move(0.0f, -0.015f);
pacmanMC_sprite.setRotation(-90.f);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Down))
{
pacmanMC_sprite.move(0.0f, 0.015f);
pacmanMC_sprite.setRotation(90.f);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left))
{
pacmanMC_sprite.move(-0.015f, 0.0f);
pacmanMC_sprite.setRotation(-180.f);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right))
{
pacmanMC_sprite.move(0.015f, 0.0f);
pacmanMC_sprite.setRotation(0);
}
renderWindow.clear();
renderWindow.draw(background_sprite);
renderWindow.draw(pacmanMC_sprite);
renderWindow.display();
}
return 0;
}
I am currently trying to recreate Chess in SFML. Generating the board normally works as intended but when I am resizing the window I get weird white borders.
Before Resize:
After Resize:
It looks like the view is not aligned with the window properly so I think my problem is not in the board generation but how I am handling resizes. I thought manually updating the window view would help but it only ensured that the squares don't get streched. The border issue remains however so now I am quite clueless as to how I could fix this problem.
Board.h:
#pragma once
#include <SFML/Graphics.hpp>
class Board
{
public:
void createBoard(sf::Vector2u windowSize);
void drawBoard(sf::RenderWindow& window) const;
private:
sf::RenderTexture board;
sf::Color lightColor = sf::Color(159, 144, 176);
sf::Color darkColor = sf::Color(125, 74, 141);
sf::Color backColor = sf::Color(32, 31, 32);
};
Board.cpp:
#include <SFML/Graphics.hpp>
#include "Board.h"
void Board::createBoard(sf::Vector2u windowSize)
{
const float xOffset = static_cast<float>(windowSize.x - windowSize.y) / 2.f;
const float squareSize = static_cast<float>(windowSize.y) / 8.f;
board.create(windowSize.x, windowSize.y);
board.clear(backColor);
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
sf::RectangleShape currSquare({ squareSize, squareSize });
currSquare.setFillColor((i + j) % 2 ? lightColor : darkColor);
currSquare.setPosition(xOffset + static_cast<float>(i) * squareSize, (static_cast<float>(j) * squareSize));
board.draw(currSquare);
}
}
}
void Board::drawBoard(sf::RenderWindow& window) const
{
window.draw(sf::Sprite(board.getTexture()));
}
main.cpp:
#include <SFML/Graphics.hpp>
#include "Board.h"
int main()
{
sf::RenderWindow window(sf::VideoMode(500, 300), "Chess");
sf::Event event;
sf::View view = window.getDefaultView();
Board board;
board.createBoard(window.getSize());
while (window.isOpen()) {
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
else if (event.type == sf::Event::Resized) {
view.setSize({ static_cast<float>(event.size.width), static_cast<float>(event.size.height) });
window.setView(view);
board.createBoard({ event.size.width, event.size.height });
}
}
window.clear(sf::Color::White);
board.drawBoard(window);
window.display();
}
return 0;
}
Does anyone know how I could fix this problem?
The problem is that you don't only need to resize the view, but also recenter it. As right now you are not doing it, the center remains where the smaller board was and the bigger view takes a chunk from the outside in the top left corner.
So just change your code like this:
...
else if (event.type == sf::Event::Resized) {
float w = static_cast<float>(event.size.width);
float h = static_cast<float>(event.size.height);
view.setSize({w , h});
view.setCenter({w/2.f , h/2.f}); // <----- !
window.setView(view);
board.createBoard({ event.size.width, event.size.height });
}
...
I need a double buffer because i'm starting to notice tearing when I want to move my texture made tile map around the screen via mouse click.
I'm using SDL2 and this is a SDL2 specific question, check out my code that produces tearing, whats wrong?
//set up buffer and display
bufferTexture = SDL_CreateTexture(renderer,SDL_PIXELFORMAT_RGBA8888,SDL_TEXTUREACCESS_STREAMING,800,600);
displayTexture = SDL_CreateTexture(renderer,SDL_PIXELFORMAT_RGBA8888,SDL_TEXTUREACCESS_TARGET,800,600);
while(running)
{
handleEvents();
SDL_SetRenderTarget(renderer,bufferTexture); // everything goes into this buffer
SDL_RenderClear(renderer); // clear buffer before draw
gTileMovement.updateMapCoordinates();
for(int i = 0; i < MAP_ROWS; i++)//rows
{
for(int j = 0; j < MAP_COLUMNS; j++)//columns
{
x = (j * 100) - (i * 100);
y = ((i * 100) + (j * 100)) / 2;
drawTiles(i,j,x,y);
}
}
//move from buffer to display texture
memcpy(&displayTexture,&bufferTexture,sizeof((&bufferTexture)+1));
//change render target back to display texture
SDL_SetRenderTarget(renderer,displayTexture);
//show it all on screen
SDL_RenderPresent(renderer);
}
for all it matters here is my drawTiles function too, is this not conventional? :
void drawTiles(int i,int j,int x,int y)
{
//updates based on a mouse clicks xy coords
gTileMovement.updateMapCoordinates();
if(tileMap[i][j] == 1) // grass?
{
gSpriteSheetTexture.render(x+gTileMovement.getUpdatedX(),y+gTileMovement.getUpdatedY(),&gSpriteClips[1]);
}
if(tileMap[i][j] == 0) // wall?
{
gSpriteSheetTexture.render(x+gTileMovement.getUpdatedX(),y+gTileMovement.getUpdatedY(),&gSpriteClips[0]);
}
if(tileMap[i][j] == 2) // tree?
{
gSpriteSheetTexture.render(x+gTileMovement.getUpdatedX(),y+gTileMovement.getUpdatedY(),&gSpriteClips[2]);
}
}
Which followes into how I SDL_RenderCopy the tiles through a class. This copies the textures onto the current targeted renderer does it not? Which is the buffer texture if i'm not mistaken.
void LTexture::render(int x, int y, SDL_Rect * clip)
{
SDL_Rect renderQuad = {x, y, mWidth, mHeight};
if(clip != NULL)
{
renderQuad.w = clip->w;
renderQuad.h = clip->h;
}
SDL_RenderCopy(renderer, mTexture, clip, &renderQuad);
}