While trying to port a game engine to SFML2 originally made in 1.6 I've run into some problems. There are no compile errors but some tiles are not rendered and are instead left completely black. Here is the original code (works perfectly), the 2.0 port, and my typed out notes as I was porting. If any clarification is needed to help out let me know. Thanks for your time!
SFML 1.6 (Works perfectly)
header
/*
* RenderSystem.h
* GameEngine
*
* Created by Jesse Werner on 1/30/11.
* Copyright 2011 none. All rights reserved.
*
*/
#pragma once
#include <SFML/Graphics.hpp>
#include <vector>
#include "TileLayer.h"
using namespace sf;
using namespace std;
class RenderSystem
{
public:
RenderSystem(RenderWindow *myRenderWindow, float myRatio);
RenderWindow *renderWindow;
vector<Sprite*> renderList;
vector<TileLayer*> tileLayers;
float PTM_RATIO;
void render(View *view);
};
SFML 1.6 (works perfectly) cpp
/*
* RenderSystem.cpp
* GameEngine
*
* Created by Jesse Werner on 1/30/11.
* Copyright 2011 none. All rights reserved.
*
*/
#include <SFML/Graphics.hpp>
#include "RenderSystem.h"
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace sf;
RenderSystem::RenderSystem(RenderWindow *myRenderWindow, float myRatio)
{
renderWindow = myRenderWindow;
PTM_RATIO = myRatio;
}
void RenderSystem::render(View *view)
{
//printf("view center: %f,%f \n",view->GetCenter().x, view->GetCenter().y);
//printf("view extents: %f,%f \n",view->GetHalfSize().x, view->GetHalfSize().y);
for(int i = 0; i<tileLayers.size(); i += 1)
{
for(int j = 0; j<tileLayers[i]->tiles.size(); j++)
{
Tile* tile = tileLayers[i]->tiles[j];
//printf("(%f,%f) \n",tile->x * PTM_RATIO, tile->y * PTM_RATIO);
if(tile->x*PTM_RATIO - view->GetCenter().x < view->GetHalfSize().x )
{
if(tile->x*PTM_RATIO - view->GetCenter().x > -view->GetHalfSize().x-1*PTM_RATIO)
{
if(tile->y*PTM_RATIO - view->GetCenter().y < view->GetHalfSize().y )
{
if(tile->y*PTM_RATIO - view->GetCenter().y > -view->GetHalfSize().y-1*PTM_RATIO )
{
Image *src = tileLayers[i]->image;
int left = tile->tx;
int right = left + 16;
int top = tile->ty;
int bottom = tile->ty + 16;
Sprite sprite;
sprite.SetImage(*src);
sprite.SetSubRect(IntRect( left, top, right, bottom));
sprite.SetX(tile->x*PTM_RATIO);
sprite.SetY(tile->y*PTM_RATIO);
renderWindow->Draw(sprite);
}
}
}
}
}
}
for(int i = 0; i<renderList.size(); i++)
{
renderWindow->Draw(*renderList[i]);
}
renderList.clear();
}
SFML2 (does not work)
header
/*
* RenderSystem.h
* GameEngine
*
* Created by Jesse Werner on 1/30/11.
* Copyright 2011 none. All rights reserved.
*
*/
#pragma once
#include <SFML/Graphics.hpp>
#include <vector>
#include "TileLayer.h"
using namespace sf;
using namespace std;
class RenderSystem
{
public:
RenderSystem(RenderWindow *myRenderWindow, float myRatio);
RenderWindow *renderWindow;
vector<Sprite*> renderList;
vector<TileLayer*> tileLayers;
float PTM_RATIO;
void render(View *view);
};
SFML2 (does not work)
cpp
/*
* RenderSystem.cpp
* GameEngine
*
* Created by Jesse Werner on 1/30/11.
* Copyright 2011 none. All rights reserved.
*
*/
#include <SFML/Graphics.hpp>
#include "RenderSystem.h"
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace sf;
RenderSystem::RenderSystem(RenderWindow *myRenderWindow, float myRatio)
{
renderWindow = myRenderWindow;
PTM_RATIO = myRatio;
}
void RenderSystem::render(View *view)
{
//printf("view center: %f,%f \n",view->GetOrigin().x, view->GetOrigin().y);
//printf("view extents: %f,%f \n",view->GetSize()/2().x, view->GetSize()/2().y);
for(int i = 0; i<tileLayers.size(); i += 1)
{
for(int j = 0; j<tileLayers[i]->tiles.size(); j++)
{
Tile* tile = tileLayers[i]->tiles[j];
//printf("(%f,%f) \n",tile->x * PTM_RATIO, tile->y * PTM_RATIO);
if(tile->x*PTM_RATIO - view->GetCenter().x < view->GetSize().x/2.0 )
{
if(tile->x*PTM_RATIO - view->GetCenter().x > -view->GetSize().x/2.0-1*PTM_RATIO)
{
if(tile->y*PTM_RATIO - view->GetCenter().y < view->GetSize().y/2.0 )
{
if(tile->y*PTM_RATIO - view->GetCenter().y > -view->GetSize().y/2.0-1*PTM_RATIO )
{
Image *src = tileLayers[i]->image;
int left = tile->tx;
int right = left + 16;
int top = tile->ty;
int bottom = tile->ty + 16;
Sprite sprite;
sprite.SetImage(*src);
sprite.SetSubRect(IntRect( left, top, right, bottom));
sprite.SetX(tile->x*PTM_RATIO);
sprite.SetY(tile->y*PTM_RATIO);
renderWindow->Draw(sprite);
}
}
}
}
}
}
for(int i = 0; i<renderList.size(); i++)
{
renderWindow->Draw(*renderList[i]);
}
renderList.clear();
}
Changes made (sorry for lack of readability)
sprite class
getcenter ---> getorigin
setcenter ---> setorigin
view class
gethalfsize ---> getsize()/2
vectorf's must be redone because no more halfsize
string class
no longer has setposition function
renderwindow class
enable useverticalsync to EnableVerticalSync
windowsettings to contextsettings class
ensure defining SFML_STATIC