Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SFML 1.6 to SFML 2.0 porting failure  (Read 3752 times)

0 Members and 1 Guest are viewing this topic.

JCSopko

  • Newbie
  • *
  • Posts: 19
    • View Profile
SFML 1.6 to SFML 2.0 porting failure
« on: February 27, 2011, 02:43:55 am »
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
Code: [Select]
/*
 *  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
Code: [Select]
/*
 *  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
Code: [Select]
/*
 *  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
Code: [Select]
/*
 *  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)
Code: [Select]

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

JCSopko

  • Newbie
  • *
  • Posts: 19
    • View Profile
SFML 1.6 to SFML 2.0 porting failure
« Reply #1 on: March 03, 2011, 07:36:31 pm »
No one? =[

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
SFML 1.6 to SFML 2.0 porting failure
« Reply #2 on: March 03, 2011, 08:57:14 pm »
sf::Rect now stores left, top, width, height instead of left, top, right, bottom.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

WitchD0ctor

  • Full Member
  • ***
  • Posts: 100
    • View Profile
    • http://www.teleforce-blogspot.com
SFML 1.6 to SFML 2.0 porting failure
« Reply #3 on: March 04, 2011, 12:53:34 am »
What Nexus said, basically what that means for you is that instead of
Code: [Select]

int left = tile->tx;
int right = left + 16;
int top = tile->ty;
int bottom = tile->ty + 16;


for right and bottom, you can just do

Code: [Select]

int left = tile->tx;
int right = 16;
int top = tile->ty;
int bottom = 16;
John Carmack can Divide by zer0.

JCSopko

  • Newbie
  • *
  • Posts: 19
    • View Profile
SFML 1.6 to SFML 2.0 porting failure
« Reply #4 on: March 07, 2011, 08:26:55 pm »
Thank you so much both of you!

Everything's working now with the exception of a ghosting issue of sorts. I may take video to demonstrate.