I'm rather new at SFML and I've been playing around with a basic tile engine that I found online. In the tile engine was a camera that did not work all that well and so I took it out with the intent to replace it with my own later. Directly prior to this, the engine would draw only the tiles which were on screen and for some reason was incapable of adding additional tiles to the list of the tiles which need to be drawn. At the current time, I am attempting to draw every tile possible but receiving a vector subscript error after the first row - 1.
Here is the code that seems to be causing the error:
int levelHeight = currentLevel->getHeight();
int levelWidth = currentLevel->getWidth();
for(int tileY = 1; tileY < levelHeight; tileY++)
{
for(int tileX = 1; tileY < levelWidth; tileX++)
{
tile = currentLevel->getTile(tileX, tileY);
std::cout << "Adding Tile X: " << tileX << ", Y: " << tileY << " to buffer." << std::endl;
if(tile)
tile->draw((tileX * tileSize), (tileY * tileSize), display);
}
}
If there is anything else that it would be useful if I provide feel free to ask
I think the inner for loop terminating condition is incorrect:
for(int tileX = 1; tileY < levelWidth; tileX++)
should be:
for(int tileX = 1; tileX < levelWidth; tileX++)
//^
Related
So for my university homework we are supposed to make a simple game of a 2D map with entities etc.
So I've figured a way of printing a map through it's dimensions and text formatting yet in our lessons it wasn't mentioned how we print on specific parts of the terminal. I've checked same questions but can't seem to get a solution.
Here is the code I use to print the map and make it's array. BLUE_B,STANDARD_B,OUTLINE and GREEN_B are declared above for the sake of color enrichment. Also IF POSSIBLE I don't want to use OS specific commands unless it's completely necessary. I use VS Code for Windows, compile with g++ on WSL Ubuntu-20.04.
for (int row = 0; row < i; row++) {
cout << OUTLINE "##";
for (int column = 0; column < j; column++) {
int n = rand() % 10; // According to "rand()"'s value we print either green, blue, or trees
if (n >= 3) { // We've assigned more values to green, in order to be more possible to be printed
cout << GREEN_B " "
STANDARD_B;
map[row][column] = 1;
} else if (n == 0 || n == 1) {
cout << BLUE_B " "
STANDARD_B;
map[row][column] = 0;
} else if (n == 2) {
int tree = rand() % 2;
cout << TREES "<>"
STANDARD_B;
map[row][column] = 0;
}
}
cout << OUTLINE "##"
STANDARD_B << endl;
}
for (i = 0; i < j + 2; i++) { // Bottom map border printing
cout << OUTLINE "##"
STANDARD_B;
}
If I understand the question correctly, you might be looking for iomanip. It is just one way of doing it. You can use setw and setfill to position different text in different areas. You can set different options for different outputs.
To move the text cursor to a specific line and column you need a “gotoxy”-style function.
Here is something that will work on both Linux terminals and the Windows Terminal. (It will not work on Windows Console without additional initialization help.)
#include <iostream>
const char * CSI = "\033[";
void gotoxy( int x, int y )
{
std::cout << CSI << (y+1) << ";" << (x+1) << "H";
}
Coordinates are (0,0) for the UL corner of the terminal. Here is a working example of use:
// continuing from above
#include <string>
int main()
{
// Clear a 40 x 10 box
for (int y = 0; y < 10; y++)
{
gotoxy( 0, y );
std::cout << std::string( 40, ' ' );
}
// Draw our centered text
gotoxy( 14, 5 );
std::cout << "Hello there!";
// Go to bottom of box and terminate
gotoxy( 0, 10 );
std::cout.flush();
}
For your game
I suggest you move the cursor to HOME (0,0) and draw the changed parts of your gameboard each frame.
I suppose that if you are on a local computer and your gameboard is relatively simple, you could probably get away with a complete redraw each frame...
Are you sure there is no professor-supplied macro or command to move the cursor home? (...as he has supplied magic macros to change the output color)?
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.
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.
I have a function that returns a boolean. this function when compiled seems to contain nothing, and will always return true while also skipping over all the calls to cout or cin that i put in it. to see what it's actually doing. What is going on and how do i fix this issue.
In my process of troubleshooting, i have,
used GDB with a breakpoint at object::collides, this resulted in the function being called but not outputting anything to the console
Numbered my objects to and compared what objects the program thinks are colliding to the objects that are colliding. if it passes the proximity test, the program thinks the objects are colliding, evidence that it is always returning true.
tried various other methods to try to figure out what is going on, but all have left my without answers
in object.cpp:
bool object::collides(object * other)
{
std::vector<point> a_pnt = getBounds();
std::vector<point> b_pnt = other->getBounds();
for (int i = 0; i < a_pnt.size(); i++)
{
for (int j = 0; j < b_pnt.size(); j++)
{
point v1 = a_pnt[i];
point v2 = a_pnt[(i+1)%a_pnt.size()];
point v3 = b_pnt[j];
//edit: fixed typo
point v4 = b_pnt[(j+1)%b_pnt.size()];
double num_1 = ((v3.x - v1.x) * -(v4.y - v3.y)) - (-(v4.x - v3.x) * (v3.y - v1.y));
double num_2 = ((v2.x - v1.x) * (v3.y - v1.y)) - ((v3.x - v1.x) * (v2.y - v1.y));
double den =((v2.x - v1.x) * -(v4.y - v3.y)) - (-(v4.x - v3.x) * (v2.y - v1.y));
double frac_1 = num_1 / den;
double frac_2 = num_2 / den;
//debug code start
std::cout << num_1 << "/" << den << "=" << frac_1 << std::endl;
std::cout << num_2 << "/" << den << "=" << frac_2 << std::endl;
std::cout << (frac_1 > 0.0) << " " << (frac_1 < 1.0) << " " << (frac_2 > 0.0) << " " << (frac_2 < 1.0) << std::endl;
std::cout << std::endl;
std::string hahah;
std::cin >> hahah;
//end debug code
//edit: fixed conditional
if((frac_1>0.0)&&(frac_1<1.0)&&(frac_2>0.0)&&(frac_2<1.0));
return true;
}
}
//edit: fixed conditional
return false;
}
in mode.cpp in function mode::step():
for (int i = 0; i<onScreen.size(); i++)
{
object * o1 = onScreen[i];
for(int j = i+1; j<onScreen.size(); j++)
{
object * o2 = onScreen[j];
if(o1->getVectorLength(o2)<50){
std::cout << "Checking collisions for objects " << i << " and " << j << std::endl;
if(o1->collides(o2))
{
std::cout << "somthing collided\n";
}
}
}
}
output:
Checking for Collisions
Checking collisions for objects 0 and 11
somthing collided
Checking collisions for objects 1 and 8
somthing collided
Checking collisions for objects 1 and 18
somthing collided
Checking collisions for objects 1 and 26
somthing collided
Expected results is for the "collides" function to output to the screen or request the input for that string, this will show that it is actually going through that section of code properly. however it doesn't do this. the "collides" function returns true regardless of whether or not the actual intersect section is true or false, while skipping over all of my debug code, as shown in the output.
edits:
fixed the return in collides
fixed a typo
still doesn't work.
does go thought loops with bullet/bullet combinations not bullet/asteroid or asteroid/asteroid
checking getBounds has me scratching my head...
std::vector asteroid::getBounds()
{
//my issue was here, check your functions a bit more closely :P
//wasn't returning a vector with anything in it.
std::vector t;
//now it's
std::vector t = lyrs[0].pnts;
for (int i = 0; i < t.size(); i++)
{
double x = t[i].x+location.x;
double y = t[i].y+location.y;
t[i] = point{x, y, t[i].z};
}
return t;
}
i thought that was implemented properly
The problem occurs on this line:
if((frac_1>0.0)&&(frac_1<1.0)&&(frac_2>0.0)&&(frac_2<1.0)); //This semicolon here
return true;
Putting a semicolon at the end of the if statement basically ends the if statement. What you wrote is equivalent to
if((frac_1>0.0)&&(frac_1<1.0)&&(frac_2>0.0)&&(frac_2<1.0))
{
}
return true;
Fixing it is pretty simple. Just remove the semicolon:
if((frac_1>0.0)&&(frac_1<1.0)&&(frac_2>0.0)&&(frac_2<1.0))
return true;
So my issue was simple, and a bit of a "doh" moment. First the unrealtated issue I had to fix, was a return true, regardless of whether or not my math was actually done correctly, but as this section wasn't being hit, this wasn't the real issue. Thanks for those that noticed it anyway.
Issue one (no if on return true):
In collides.cpp
for (int i = 0; i < a_pnt.size(); i++)
{
for (int j = 0; j < b_pnt.size(); j++)
{
...
return true;
}
}
Fixed to:
for (int i = 0; i < a_pnt.size(); i++)
{
for (int j = 0; j < b_pnt.size(); j++)
{
...
if((frac_1>0.0)&&(frac_1<1.0)&&(frac_2>0.0)&&(frac_2<1.0))
return true;
}
}
The second issue, and the main one, after a suggestion from a commenter, his name is up in the question above, I double checked my getter for the bounding boxes. Low and behold, that was my issue. While at first I discounted his advice, because I thought I had fully implemented that getter, it was my issue and learning valuable lessons is always a good thing.
Issue two(incomplete implementation of GetBounds, resulted in empty vector getting returned.):
in asteroids.cpp:
std::vector asteroid::getBounds()
{
//my issue was here, check your functions a bit more closely :P
//wasn't returning a vector with anything in it.
std::vector<point> t;
//now it's
std::vector<point> t = lyrs[0].pnts;
for (int i = 0; i < t.size(); i++)
{
double x = t[i].x+location.x;
double y = t[i].y+location.y;
t[i] = point{x, y, t[i].z};
}
return t;
}
Lesson to learn: Even when you think you have everything working correctly there are times when you don't and you should check and double check EVERY function you are calling just incase one of those functions you think is working isn't actually working as it should.
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.