Commit f6c5d422 authored by alaskalinuxuser's avatar alaskalinuxuser

Section 7 completed. Added background tiles.

parent 4dcdc657
#include <sstream>
#include <SFML/Graphics.hpp>
#include "JelloStorm.h"
int createBackground(VertexArray& rVA, IntRect arena)
{
// Anything we do to rVA we are actually doing to background (in the main function)
// How big is each tile/texture
const int TILE_SIZE = 50;
const int TILE_TYPES = 3;
const int VERTS_IN_QUAD = 4;
int worldWidth = arena.width / TILE_SIZE;
int worldHeight = arena.height / TILE_SIZE;
// We are using quads, right?
rVA.setPrimitiveType(Quads);
// Set the size of the vertex array
rVA.resize(worldWidth * worldHeight * VERTS_IN_QUAD);
// Start at the beginning of the vertex array
int currentVertex = 0;
for (int w = 0; w < worldWidth; w++)
{
for (int h = 0; h < worldHeight; h++)
{
// Position each vertex in the current quad
rVA[currentVertex + 0].position = Vector2f(w * TILE_SIZE, h * TILE_SIZE);
rVA[currentVertex + 1].position = Vector2f((w * TILE_SIZE) + TILE_SIZE, h * TILE_SIZE);
rVA[currentVertex + 2].position = Vector2f((w * TILE_SIZE) + TILE_SIZE, (h * TILE_SIZE) + TILE_SIZE);
rVA[currentVertex + 3].position = Vector2f((w * TILE_SIZE), (h * TILE_SIZE) + TILE_SIZE);
// Define the position in the Texture to draw for current quad
// Either grass, daisy, stick or wall
if (h == 0 || h == worldHeight - 1 || w == 0 || w == worldWidth - 1)
{
// Use the boarder texture
rVA[currentVertex + 0].texCoords = Vector2f(0, 0 + TILE_TYPES * TILE_SIZE);
rVA[currentVertex + 1].texCoords = Vector2f(TILE_SIZE, 0 + TILE_TYPES * TILE_SIZE);
rVA[currentVertex + 2].texCoords = Vector2f(TILE_SIZE, TILE_SIZE + TILE_TYPES * TILE_SIZE);
rVA[currentVertex + 3].texCoords = Vector2f(0, TILE_SIZE + TILE_TYPES * TILE_SIZE);
}
else
{
// Use a random floor texture
srand((int)time(0) + h * w - h);
int mOrG = (rand() % TILE_TYPES);
int verticalOffset = mOrG * TILE_SIZE;
rVA[currentVertex + 0].texCoords = Vector2f(0, 0 + verticalOffset);
rVA[currentVertex + 1].texCoords = Vector2f(TILE_SIZE, 0 + verticalOffset);
rVA[currentVertex + 2].texCoords = Vector2f(TILE_SIZE, TILE_SIZE + verticalOffset);
rVA[currentVertex + 3].texCoords = Vector2f(0, TILE_SIZE + verticalOffset);
}
// Position ready for the next for vertices
currentVertex = currentVertex + VERTS_IN_QUAD;
}
}
return TILE_SIZE;
}
......@@ -2,6 +2,7 @@
#include <sstream>
#include <SFML/Audio.hpp>
#include <Player.h>
#include <JelloStorm.h>
// Declare the functions....
......@@ -32,17 +33,6 @@ int main() {
// Create a an SFML View for the main action
View mainView(FloatRect(0, 0, resolution.x, resolution.y));
// Create a texture for our background and load it.
Texture textureBackground;
textureBackground.loadFromFile("graphics/background.png");
// Now create a sprite and attach our background texture to it.
Sprite spriteBackground;
spriteBackground.setTexture(textureBackground);
// Give the spritebackground a position.
spriteBackground.setPosition(0,0);
// Time control
Clock clock;
......@@ -60,6 +50,11 @@ int main() {
// Create an arena, or game area.
IntRect arena;
// Create our background tiles
VertexArray tileBackground;
Texture textureBackground;
textureBackground.loadFromFile("graphics/background_sheet.png");
while (window.isOpen()) {
......@@ -201,7 +196,7 @@ int main() {
arena.top = 0;
// We will modify this line of code later
int tileSize = 50;
int tileSize = createBackground(tileBackground, arena);
// Spawn the player in the middle of the arena
player.spawn(arena, resolution, tileSize);
......@@ -259,6 +254,8 @@ int main() {
// set the mainView to be displayed in the window
// And draw everything related to it
window.setView(mainView);
window.draw(tileBackground, &textureBackground);
// Draw the player
window.draw(player.getSprite());
......@@ -272,7 +269,6 @@ int main() {
if (state == State::PAUSED)
{
window.clear();
window.draw(spriteBackground);
}
if (state == State::GAME_OVER)
......
#pragma once
using namespace sf;
int createBackground(VertexArray& rVA, IntRect arena);
No preview for this file type
No preview for this file type
......@@ -2,10 +2,11 @@
# You need libsfml installed on your system!
g++ -I./ -fpermissive -Wall -Wextra -g -c -std=c++11 Player.cpp -o Player.o
g++ -I./ -fpermissive -Wall -Wextra -g -c -std=c++11 JelloStorm.cpp -o JelloStorm.o
g++ -I./ -Wall -Wextra -g -c -std=c++11 Player.cpp -o Player.o
g++ -I./ -Wall -Wextra -g -c -std=c++11 CreateBackground.cpp -o CreateBackground.o
g++ -I./ -Wall -Wextra -g -c -std=c++11 JelloStorm.cpp -o JelloStorm.o
g++ JelloStorm.o Player.o -o JelloStorm-app -lsfml-graphics -lsfml-window -lsfml-system -lsfml-audio
g++ JelloStorm.o Player.o CreateBackground.o -o JelloStorm-app -lsfml-graphics -lsfml-window -lsfml-system -lsfml-audio
./JelloStorm-app
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment