Indicating ships are shot down in game in C++ - c++

I am writing the battleship game in C++. I have set the values as below.
//const int empty = 0; // contains water
//const int occupied = 1; // contains a ship
//const int missed = 2; // shot into ocean W
//const int shot = 3; // ship is shot down H
//const int shipdown = 4; // whole ship is shot down S
When the user hit the ship, the value is changed from 1 to 3. The problem i face is how do I indicate that the whole ship is down.
int Grid[64];
int currentSquareValue = Grid[(row-1)*8+(column-1)];
switch(currentSquareValue)
{
case 0:
Grid[(row-1)*8+(column-1)] = 2;
break;
case 1:
Grid[(row-1)*8+(column-1)] = 3;
break;
default:
break;
}
//Printing out the Grid
for(int i = 0 ; i <=8; i++)
{
//Print Row Header
if(i != 0) cout << i << char(179);
for(int j = 0; j <= 8; j++)
{
if(i == 0)
{
//Print Column Header
cout << j << char(179);
}
else
{
//Avoid the first row and column header
if(j > 0 && i > 0)
{
int currentSquareValue = Grid[(i-1)*8+(j-1)];
switch(currentSquareValue)
{
case 0:
cout << " " << char(179);
break;
case 1:
cout << " " << char(179);
break;
case 2:
cout << "W" << char(179);
break;
case 3:
cout << "H" << char(179);
break;
case 4:
cout << "S" << char(179);
break;
default:
break;
}
}
}
}
I have already done the ship being hit, but I am not sure how to show that the whole ship is shot down after the third shot like in this:
Need some guidance on this... Not sure how to start on this..

A simple solution, not necessarily the best. Make your grid of a struct instead of an int. This struct contains a flag for whether a ship is present, the ID of a ship there, (this lets you tell them apart; if no ship is present the value shouldn't be used) and a separate flag for whether the cell has been hit. Now make an array for each ship ID in your game, which contains the number of cells that make up your ship. So [0] -> 3, means ship ID 0 takes up 3 squares. Whenever a new hit is registered against a cell containing this ship ID, decrease the value in the array. When it is 0, you know the whole ship has been hit.

You need to either store the coordinates of the ships in another data structure (so you can find the ship from a hit, and then mark all its squares as sunk) or make your matrix store more complex data (a ship ID) so you can mark all cases for that ship as sunk.
The latter would give you data such as:
const unsigned int empty = 0x0000;
const unsigned int shipIdMask = 0x00FF;
const unsigned int hitFlag = 0x0100;
const unsigned int sunkFlag = 0x0200;
Display-wise, you just do a if((value & shipIdMask) != 0) to check if there is a ship in there, and you check hits likewise. When a ship is hit, you can go the lazy way and simply sweep the entire matrix for squares with the same ship ID. If all of them are hit, you sweep them again and mark them all as sunk.
Both techniques can be combined if you don't want to sweep the whole matrix each time (use the ship ID to get the ship's actual coordinates in an array).

Maybe you could consider a bitboard representation of the board. Typically I've seen it used for chess, but since your board has 64 squares it seems appropriate here as well. The basic idea is that each location on the grid is represented by exactly one bit in a 64 bit int. Then operations can be performed quickly and easily via bit manipulation. In that type of representation, you would determine if a ship is sunk via something like this:
bool is_sunk(uint64_t board, uint64_t ship) {
return board & ship == ship;
}
And other operations, are equally easy.
For example, is a ship hit?
bool is_hit(uint64_t board, uint64_t ship) {
return board & ship != 0;
}
Did I win the game?
bool is_won(uint64_t board, uint64_t* ships, int size) {
uint64_6 opponents_ships = 0;
for (int i = 0; i < size; i++) opponents_ships |= *ships;
return is_sunk(board, opponents_ships);
}
Apply a move to the board:
bool make_move(uint64_t& board, uint64_t move) {
board &= move;
}

I suggest you make your grid cells have information, such as a pointer to the ship at that location.
Another idea is to have a container of ships and each ship will contain the coordinates of each of it's cells (locations). The ship would contain the status of those cells (visible, hit, sunk, etc.).

Related

Spiral Iteration

I need an Algorithm that I will use to scan pixels out from the center. Problem is with different lengths and sizes, it sometimes can't get to the position (See Image below blue part).
To illustrate the problem more I will show the example output:
If you compare the pictures you will notice that it goes in a spiral and the outputs match with a regular for loop and obviously the problem that it doesn't print the blue part correctly
Here is the code:
#include<iostream>
#include<string>
#include<math.h>
int arr[] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 };
int arrSize = sizeof(arr) / sizeof(arr[0]);
int width = 5;
int height = 3;
void normal2DArray() {
int index = 0;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
std::cout << std::to_string(x) << "," << std::to_string(y) << " = " << std::to_string(arr[index]) << "\n";
index++;
}
}
}
int convertToInex(int x, int y) {
int left = x * y; // elements to the left
int right = (width - x) * y; // elements to the right
return left + right + x;
}
void spiralArray() {
// calculate middle point, which is also the start point
int x = round((float)width / 2) - 1;
int y = round((float)height / 2) - 1;
int direction = 0; // 0=right, 1=up, 2=left, 3=down
int turnCounter = 1;
int numSteps = 1;
int step = 1;
int index;
while (true) {
index = convertToInex(x, y); // defines the index position in arr
std::cout << std::to_string(x) << "," << std::to_string(y) << " = " << std::to_string(arr[index]) << "\n";
switch (direction) {
case 0: x++; break;
case 1: y--; break;
case 2: x--; break;
case 3: y++; break;
}
index = convertToInex(x, y);
if (step % numSteps == 0) {
direction = (direction + 1) % 4;
turnCounter++;
if (turnCounter % 2 == 0) numSteps++;
}
step++;
if (step > arrSize) break;
}
}
void main() {
std::cout << "Output of Normal 2D Array:\n";
normal2DArray();
std::cout << "\n"; // better spacing
std::cout << "Output of Spiral Array:\n";
spiralArray();
}
I tried to keep the code as simple and small as possible. It should be ready to import and use.
And yes I already searched for my answer online but I didn't find anything that covered up the problem here nor had a similar setup like I have(1D arr and combined 2D array WIDTH/HEIGHT) and for sure not in c++.
❗ Also I need a Solution that works with all widths and heights and arr sizes and also works for any side ❗
I hope you can provide me with helpful answers and would be grateful with good and fast algorithm implementations/optimizations
EDIT:
Thanks to the replies in this Thread. I decided to go with the solution from #ldog for now even though I'm not completely satisfied with it.
Here are the edited code parts:
int failcounter = 0;
while (true) {
index = convertToInex(x, y); // defines the index position in arr
if (index < 0 || index > arrSize) failcounter++;
else std::cout << std::to_string(x) << "," << std::to_string(y) << " = " << std::to_string(arr[index]) << "\n";
// unchanged code inbetween
if (step > arrSize + failcounter) break;
Based on your comment:
#Beta they don't need to connect. It just has to detect that it's outside the array size (in that case -1) and don't scan them and find the next continue point. So it would continue like this: 5, 1, 6, 11
it seems you don't care that the spiral goes "out-of-bounds". In this case, the trivial answer is, embed the shapes that have no spiral in one that is always guaranteed to have one.
Thus if your input rectangle is N x M, then embed it in a rectangle of size max(M,N) x max(M,N), solve the problem in the latter, and when printing just ignore non-existent numbers in the original problem. Your printed sequence then will always be unique up to how the embedding occurs. The most reasonable embedding would try to center the smaller rectangle as much as possible in the larger rectangle, but this is up to you.
In this case you don't need an algorithm as you can compute everything analytically if you care to do the book-keeping and figure out the formulas involved.
You can hit a dead end (meaning exit the grid) in four spots. In each case, jump to the next live pixel you would have reached, if any live cells remain.
You can do this fairly easily by keeping track of the four corners you've visited furthest from the starting pixel. Using compass coords and N for up, these are the NE, NW, SW, and SE extremes visited.
If you hit a dead end going N from the NE pixel, jump to the pixel one to the left of the NW pixel and set the movement direction to down. If that is also a dead end, jump to one below the SW pixel and set the movement direction to right. Etc... When all four corners and dead ends then you're done.

(C++) I have to use magic numbers to display values in the correct position in an array. How can I get rid of it?

Sorry if the code is too long. I just wanted to give a detailed description of the situation I am facing
I'm coding a game called Battle Ship. The following code is a simpler version, I did eliminate all the unnecessary logic, cause I just want to indicate the problem with the magic numbers.
Here are my struct and enum
// the dimension of the 10 + 2 game grid for detection of the neighboring ship
// (its usage comes later in the game, not at this stage)
const int SIZE = 12;
// the number of warships
const int NBSHIP = 5;
string ships[NBSHIP] = {"Carrier", "Cruiser", "Destroyer", "Submarine", "Torpedo" };
// available warships and their size on the grid
enum Ship {
CARRIER=5,
CRUISER=4,
DESTROYER=3,
SUBMARINE=3,
TORPEDO=2,
NONE=0
};
// the different states that a grid cell can take and their display
enum State {
HIT='x',
SINK='#',
MISS='o',
UNSHOT='~'
};
// a grid cell
// the ship present on the space
// the state of the box
struct Cell {
Ship ship;
State state;
};
// the player with
// his name
// his score to determine who lost
// his game grid
struct Player {
string name;
int score;
Cell grid[SIZE][SIZE];
};
// the coordinates of the cell on the grid
// its column from 'A' to 'J'
// its line from 1 to 10
struct Coordinate {
char column;
int line;
};
// the placement of the ship on the grid
// its coordinates (E5)
// its direction 'H' horizontal or 'V' vertical from the coordinate
struct Placement {
Coordinate coordi;
char direction;
};
Basically, at the beginning of the game, I have to initialize a grid with the appropriate state for each cell (in this case, UNSHOT and NONE). Then I have to display the grid and start placing the ships. The weird thing here is I have to use "magic numbers" to place the ships in the correct position according to the player's input. But I don't even know why I need it as well as how to get rid of it.
Utilization of magic number appears in placeShip function.
void initializeGrid (Cell aGrid[][SIZE])
{
for (int i = 0; i < SIZE - 2; i++)
{
for (int j = 0; j < SIZE - 2; j++)
{
aGrid[i][j].ship = NONE;
aGrid[i][j].state = UNSHOT;
}
}
}
void displayGrid(Player aPlayer)
{
cout << endl;
cout << setw(10) << aPlayer.name << endl;
// Letter coordinates
char a = 'A';
cout << " ";
for (int i = 0; i < SIZE - 2 ; i++)
{
cout << " " << char (a+i);
}
cout << endl;
// Number coordinates
for (int i = 0; i < SIZE - 2; i++)
{
// Player
if(i + 1 >= 10) // To align the first column
cout << i + 1;
else
cout << " " << i + 1;
for (int j = 0; j < SIZE - 2; j++)
{
if (aPlayer.grid[i][j].ship) // Check if there are ships
cout << " " << (aPlayer.grid[i][j].ship);
else
cout << " " << char (aPlayer.grid[i][j].state);
}
cout << endl;
}
}
void placeShip(Cell aGrid[][SIZE], Placement aPlace, Ship aShip)
{
if (aPlace.direction == 'h' || aPlace.direction == 'H')
{
for (int i = 0; i < aShip; i++) // To change the value of a cell according to the size of the boat
{
// Utilization of magic number
aGrid[aPlace.coordi.line - 9][aPlace.coordi.column + i - 1].ship = aShip; // Horizontal so we don't change the line
}
}
else if (aPlace.direction == 'v' || aPlace.direction == 'V')
{
for (int i = 0; i < aShip; i++)
{
// Utilization of magic number
aGrid[aPlace.coordi.line + i - 9][aPlace.coordi.column - 1].ship = aShip; // Vertical so we don't change the column
}
}
}
void askPlayerToPlace(Player& aPlayer)
{
Ship battleships[NBSHIP] = { CARRIER, CRUISER, DESTROYER, SUBMARINE, TORPEDO};
for (int i = 0; i < NBSHIP; i++)
{
string stringPlace;
string stringShip = ships[i];
Ship aShip = battleships[i];
string inputDirection;
Coordinate coordi;
cout << "\n" << aPlayer.name << ", time to place your carrier of length "
<< to_string(battleships[i])
<< " (" << ships[i] << ")\n"
<< "Enter coordinates (i.e. B5): ";
cin >> stringPlace;
coordi = { stringPlace[0], int(stringPlace[1]) - 48 };
cout << "Direction: ";
cin >> inputDirection;
Placement aPlace = {
coordi,
inputDirection[0]
};
placeShip(aPlayer.grid, aPlace, aShip);
displayGrid(aPlayer);
}
}
int main()
{
Player aPlayer;
cout << "Player's name: ";
getline (cin, aPlayer.name);
initializeGrid(aPlayer.grid);
displayGrid(aPlayer);
askPlayerToPlace(aPlayer);
return 0;
}
The weird thing here is I have to use "magic numbers" to place the ships in the correct position according to the player's input.
If these are numbers, which tend to appear frequently you should define another constant for these like:
const int GRID_SIZE_MARGIN = 2;
and use it
void initializeGrid (Cell aGrid[][SIZE]) {
for (int i = 0; i < SIZE - GRID_SIZE_MARGIN; i++) {
for (int j = 0; j < SIZE - GRID_SIZE_MARGIN; j++) {
aGrid[i][j].ship = NONE;
aGrid[i][j].state = UNSHOT;
}
}
}
If there are other numbers, which appear in calculations, but can't be reasonably named, it's OK to leave the numeric values. I'll try to give you an example:
The formula to calculate degrees from radians is
deg = rad * 180° / π
For π we have a constant definition (PI), but is 180 a magic number which deserves a constant definiton?
How should we name it? HUNDREDEIGHTY_DEGREES??
So it's not always reasonable to get rid of numeric constants appearing in code.
But I don't even know why I need it as well as how to get rid of it.
That's probably part ot the task, to find out.

Moving a number in a zig zag pattern through a 2D array C++

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
//function protypes
void gameBoard(); //prints board
void diceRoll();
void move();
//global variables
int roll;
int playerOne = 0;
int playerTwo = 99;
int board[5][8] = {{33,34,35,36,37,38,39,40},
{32,31,30,29,28,27,26,25},
{17,18,19,20,21,22,23,24},
{16,15,14,13,12,11,10, 9},
{ 1, 2, 3, 4, 5, 6, 7, 8}};
void diceRoll() { //asks player to input a charater to roll a dice
srand(time(0));
roll = ((rand() % 6) + 1); //sets num to a random number between 1 and 6
}
void gameBoard(){ // prints game board
for (int i = 0; i < 5; ++i){
for (int j = 0; j < 8; ++j){
cout << board[i][j] << " ";
if (board[i][j] <= 8){
cout << " ";
}
}
cout << endl << endl;
}
}
void move(int player, int& colPos, int& rowPos){
int tempCol;
int tempRow;
int previous;
for (int i = 0; i <= 2; i++){
if(i % 2 == 1){
tempCol = colPos + roll;
colPos = tempCol;
tempCol = colPos - roll;
if(colPos > 7){
colPos = 7;
rowPos--;
}
board[rowPos][colPos] = player;
}
}
}
int main() {
int turn = 1;
int colPos1 = 0;
int rowPos2 = 4;
int colPos1 = 0;
int rowPos2 = 0;
while(winner == false){
if(turn == 1){
turn = 2; //allows to switch back and forth bewteen turns
diceRoll(player1); //rolls die
move(playerOne, colPos1, rowPos2);
gameBoard(); //prints game board
}else{
turn = 1; //allows to switch back and forth bewteen turns
diceRoll(player2); //rolls die
move(playerTwo, colPos2, rowPos2);
gameBoard(); //prints game board
}
}
return 0;
}
So the code above is for a chutes and ladders game. The program above should run without any errors. I am almost done with this code but am having issues with the move function. Here I am trying to move through a 2D array (the gameboard) as each player rolls a die. There are two issues with the current code. One after a player moves a space the space that they have previously left remains marked with the player. In addition, once it goes across through an entire row it does not advance to the next row. Thanks for your help in advance.
Note: I deleted a lot of code to make it more relevant to the question so it may have errors now.
"After a player moves a space the space that they have previously left remains marked with the player"
In move(int player, int& colPos, int& rowPos)you already know which player you are updating and the current row and column position so you can use this as an opportunity to remove the mark on the current board piece and set it back to its default value e.g. once you step into move call something like void ResetBoardPositition(int colPos, int rowPos) and then update the new position with the player marker (alternatively you could not change the game board at all and just print the player position at the draw stage since you store their positions anyway)
"Once it goes across through an entire row it does not advance to the next row"
Your code isn't doing anything to take account of the fact you need to reverse direction every time you change rows. Each time you go past column 7 you just set the column to 7 and reduce the row. This means that your next roll will immediately take you past the column threshold again, set it to 7 and reduce the row count. You'll need to use some form of direction modifier for the roll so you have something like:
int direction = 1;
if (rowPos % 2 == 1)
direction = -1;
tempCol = colPos + (roll * direction);
colPos = tempCol;
tempCol = colPos - (roll * direction);
Your check for the colPos would need to account for this too
if (colPos > 7) {
colPos = 7;
rowPos--;
} else if (colPos < 0) {
colPos = 0;
rowPos--;
}
There is also the issue that you do not account for the full roll here i.e. you set the column to the flat value meaning if you were on column 7, roll a 3 (which should put you at column 5 on the next row) you only move 1 space to the next row and stop there.
Hope these answer your main questions but I think there are some other logic issues going on here with your move code.
To answer your questions, the reason the previous spaces still have that player's name in it is because that value is never reset after the player leaves. What you could do is create an int for the current position before the player gets there, move the player to the new position, and then set the old position to the int you created. I saw that your move function has an int previous, so just use that.
Second, when I ran it, the player would go through the first row, then just move up the last column. From what I saw, your move function only manipulates tempCol and colPos, but not tempRow or rowPos. Moving those will help the player move between the rows.
Also, on a side note, I saw a few things for you to look out for. You declared your function as
void diceRoll();
But when you defined it, you wrote it like this:
void diceRoll(string name) { //asks player to input a charater to roll a dice
I would suggest rather adding in your parameters when you declare it or just declare it and define it all at once.
Second, you probably haven't messed with it much, but the checkWinner() function doesn't change the value of winner.
if (pos1 || pos2 != board[0][7]) {
winner == false;
}
else {
winner == true;
}
Using == is for checking the value, not for assigning, so take out one of them.

Checking an array for existing values in C++

I've got this project I'm working on and it is an rpg game running in the cmd. The player navigates his character through the keyboard and fights enemies to level up and so on. I'm using a 2D array as a grid/map and in the demo version everything is working OK.
Problem: Now, in the more advanced version, I have a class which is used to load game/start new game. The function, that starts a new game, basicly creates .txt save files in which the information is stored. The problem is in the function that generates an enemy list. The enemy characteristics that are being generated, and where the problem is, are the X and Y coordinates. Here is a little bit of code showing the process:
void enemyGenerator(int level)
/* Declare the random generator */
std::default_random_engine generator((unsigned)time(0));
std::uniform_int_distribution<int> coordsX(1, 28); //There are 30 rows, from which the first and the last are border
std::uniform_int_distribution<int> coordsY(1, 48); //50 columns; first and last are border
/* Declare some variables */
int x, y;
bool ready = "False";
/* Check what level is demanded, for example level 1 */
if (level == 1)
{
while(true)
{
//Generate X and Y
x = coordsX(generator);
y = coordsY(generator);
//Now where the problem appears to be
//There will be 600 enemies = 1200 coordinates, so I have declared an array in the .h file
//Called coordinates[1200] = {}; now I want to check it bottom to top if
//the newly generated coordinates are already existing, so:
for (int i = 0; i < 1200; i += 2) //1200 = array size; += 2, because we're checking x & y at once
{
if (x != coordinates[i] && y != coordinates[i + 1] && x + y != 2) //x + y can't be 2, because this is where the player starts (1, 1)
{
if (i == 1198) //If these are the last x and y in the array
{
ready = "True";
break;
//Break the for loop with status ready
}
else
{
continue;
//If it isn't the end of the array simply continue checking
}
}
else
{
ready = "False";
break;
//If the x and y match with the ones in the array, then break with status not ready
}
}
if (ready)
{
break;
//If status is ready then break the loop and assign the rest of the stats
}
else
{
continue;
//If status is not ready then continue generating random values
}
}
//Here I define the values of the stats in private variables of the class
eX = x;
eY = y;
eLVL = 1;
//etc...
}
This is the generating code. And here is how I use it:
void newGame()
....
//I've reached to the point where I want to deploy for example 10 enemies of level 1
for (int i = 0; i < 10; i++)
{
enemyGenerator(1);
//I have an already defined fileWriter (std::fstream; std::ios::out)
fileWriter << eX << " " << eY << " " << eLVL; //<< " " etc...
}
....
Everything seems logical to me, the only illogical thing is that it is not working. The output in enemyList.txt I get is for example 3 5 1 (other stats) 3 5 1 (other stats) 3 5 1 (other stats), you get it.
Question: Can you spot any error? Can you show me the right way? If more of the code is required I can even send you the source file, just for the sake of curing my headache.
The problem there is with your random generator.
You are setting the seed of the generator everytime enemyGenerator() is called with the current time. But since you call enemyGenerator multiple times in the same fraction of a second, the time value is the same, hence the random generator seed is the same everytime, which will give you the same random pattern each successive call.
Either use the same generator for all the calls
...
std::default_random_engine random_generator((unsigned)time(0));
//I've reached to the point where I want to deploy for example 10 enemies of level 1
for (int i = 0; i < 10; i++)
{
enemyGenerator(random_generator, 1);
//I have an already defined fileWriter (std::fstream; std::ios::out)
fileWriter << eX << " " << eY << " " << eLVL; //<< " " etc...
}
....
with the enemyGenerator function is defined as
void enemyGenerator(std::default_random_engine& generator, int level)
or seed your generator with a different value each time.
Edit:
Well it seems it isn't the cause of your problem but you should still consider what I wrote.

drawing a tile map with sfml

So, I've been trying to create a 'Map' class to handle the drawing of my tile maps on the window but I'm having a problem to draw all the tiles to the screen.
This is my problem: lets say i have a map of 10*10 tiles with 5 layers which sums up to 10*10*5 = 500 tiles ( of course in real time there will be more likely 10 times more tiles which would be worse )... and it is stored in a 3d array [layer][row][col] or chars, each char representing an index on my sprite sheet of tiles
so i created a 'SpriteSheet' class to handle my sprite sheets and get each sprite from the sheet by an index: spriteSheet->GetSubSprite(idx).
so in the end i have something like Draw(spriteSheet->GetSubSprite((int)mapArray[i][j][k])) which should be called for each sprite, EVERY GAME LOOP so lets say my game runs on 60 fps and i have 500 tiles to draw: the game aims to draw 500*60 = 30,000(!) tiles each second...
so as you can see im having abit of a problem here...? anybody knows how to sufficiently in terms of speed imporve this? ( and of course any kind of improvement to my current structure would be very blessed ).
So, just in case here is my SpriteSheet.cpp and my Map.cpp files, I know I have many design mistakes but please focus on my question, the design is far from finished.
#include "Map.h"
sz::Map::Map(std::string path, std::string name, std::string image) : Tmx::Map() {
ParseFile(path);
if (HasError()) {
printf("error code: %d\n", GetErrorCode());
printf("error text: %s\n", GetErrorText().c_str());
system("PAUSE");
}
else {
mapArray = new unsigned char**[GetNumLayers()];
for(int i = 0; i < GetNumLayers(); i++) {
mapArray[i] = new unsigned char*[GetLayer(i)->GetHeight()];
for(int j=0; j<GetLayer(i)->GetHeight(); j++) {
mapArray[i][j] = new unsigned char[GetLayer(i)->GetWidth()];
// [layer][row][col]
}
}
//load the array
for (int i = 0; i < GetNumLayers(); ++i) {
// Get a layer.
const Tmx::Layer *layer = GetLayer(i);
for (int y = 0; y < layer->GetHeight(); ++y) {
for (int x = 0; x < layer->GetWidth(); ++x) {
// Get a tile global id.
mapArray[i][x][y] = (char)layer->GetTileGid(x, y);
//printf("%3d", mapArray[i][x][y]);
/* need to fix later on
/****************************
// Find a tileset for that id.
const Tmx::Tileset *tileset = FindTileset(layer->GetTileGid(x, y));
if (layer->IsTileFlippedHorizontally(x, y)){
printf("h");
}else{
printf(" ");
}
if (layer->IsTileFlippedVertically(x, y)){
printf("v");
}else{
printf(" ");
}
if (layer->IsTileFlippedDiagonally(x, y)){
printf("d ");
} else {
printf(" ");
}
****************************/
}
//printf("\n");
}
//printf("\n\n");
}
}
tiles = new sz::SpriteSheet(name, image, 33, 33);
}
void sz::Map::Draw(sf::RenderWindow *rw) {
// dont know what to do T_T
}
#include "SpriteSheet.h"
sz::SpriteSheet::SpriteSheet(std::string name, std::string path, int tileW, int tileH) : sz::GameObject(name, path) {
this->tileH = tileH;
this->tileW = tileW;
numOfTiles = ((this->GetImage()->GetHeight()) / tileH) * ((this->GetImage()->GetWidth()) / tileW);
}
int sz::SpriteSheet::GetTileWidth() { return tileW; }
int sz::SpriteSheet::GetTileHeight() { return tileH; }
int sz::SpriteSheet::GetNumOfTiles() { return numOfTiles; }
sf::Sprite sz::SpriteSheet::GetSubSprite(int idx) {
if(idx < 1 || idx > numOfTiles) {
std::cout << "Incorrect index!" << std::endl;
// need return value
}
int row=0, col=0, tilesEachRow = (GetImage()->GetWidth() / tileW);
while(idx > tilesEachRow) {
idx -= tilesEachRow;
col++;
}
row = idx-1;
sz::GameObject *sub = new sz::GameObject(name, path);
/*std::cout << "tileW: " << tileW << std::endl;
std::cout << "tileH: " << tileH << std::endl;
std::cout << "row: " << row << std::endl;
std::cout << "col: " << col << std::endl;
std::cout << "tiles per row: " << tilesEachRow << std::endl;
std::cout << "(" << row*tileW << ", " << col*tileH << ", " << (row+1)*tileW << ", " << (col+1)*tileH << ")" << std::endl;*/
sub->SetSubRect(sf::IntRect(row*tileW, col*tileH, (row+1)*tileW, (col+1)*tileH));
return *sub;
}
Are you actually experiencing any speed issues?
While 30,000 tiles per second may sound like a lot, in terms of computing with a more modern PC, it may not be much of a problem.
Having said this, I can definitely think of a way to optimise what you have if outputting a single tile has a certain overhead associated with it (eg. 2 tiles takes longer to output than 1 tile that is the size of both tiles combined). I don't profess to know much in terms of SFML, but I assume there is some sort of surfaces/canvas system in place. What you could do is write a method that draws static tiles to a surface, once. Then output this surface once each frame of the game. This means there is no need to repetitively output tiles every frame in a tile by tile fashion. You just output a single image of all the static tiles combined.
I reckon you could even use this system dynamically. If a tile needs to change, simply overwrite the relevant part of this surface with the updated tile data. In other words, you leave everything that is static alone and only target areas that require updating.
I agree with Alex Z in that with modern/mainstream hardware rendering 5 screens worth of tiles should not be too slow. However the only way to know for sure is to test it.
Also you need to realize that even if your map has 1000x1000 tiles, you will only be rendering 10x10 worth of tiles per frame because the rest of the tiles are outside the screen.
If you want to make your current structure more optimized, you should store each of the sprites in your sprite sheet as seperate sprites in an array. That way you will not need to call SetSubRect everytime you need to access a single tile.