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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - excrulon

Pages: [1]
1
Graphics / sf::Image deprecated? sf::Texture? a problem with rendering
« on: January 01, 2012, 11:30:23 pm »
Quote from: "Nexus"
You first have to decide how wide the scope of a variable is. If it is used during the lifetime of an object, make it a member. If you need it only in a single function (and functions invoked by it), make it a function-local variable. In your case, I think you could use many member variables: Tmx::Map, Tmx::Tileset, sf::Texture, the container of sf::Sprites. Declare the objects (not pointers) in the Map class definition, then you can use them in all member functions.

And if you really need new, for example because you transfer ownership, consider std::unique_ptr. This is a smart pointer that automatically invokes delete if the object goes out of scope. This technique is also called RAII, you find a lot about it on the internet. It is a very important idiom in C++, because it simplifies code while making it more robust and safer.


Alright, this actually clears up quite a bit. I think I'm actually going to have some sort of ResourceManager (actually think I'm going to give your thor library a try, and that has a resource manager :D), so in that case, ownership is transferred from the map class to that resource class, right? Thus I'd need new/unique_ptr.

I don't think Map will hold my sf::Sprites... I was thinking the resource manager should hold the sf::Sprites/textures so they're all in one place, and the container of Tiles in the map class will have some reference to the sprite it uses. Sound right?

2
Graphics / sf::Image deprecated? sf::Texture? a problem with rendering
« on: January 01, 2012, 11:05:23 pm »
I solved my map loading problems... finally got it to work. Was an issue with using the wrong gid.

Could you help me understand how else to do it if not using new? I thought that for textures/sprites, I *had* to allocate memory on the heap for it because if I try to pass it to a different method/container, it'd end up being white squares when I draw it unless I do that or make a copy constructor for it or something.

I'm still fuzzy on how all that works.

3
SFML projects / Thor C++ Library – An SFML extension
« on: January 01, 2012, 08:40:31 am »
I can't get cmake working with this. I read previous posts in the first few pages of this thread but it didn't help. Also read the tutorial on your site. I've installed SFML, and it's in c:\program files\sfml and my FindSFML.cmake is in C:\Program Files\SFML\cmake\Modules. CMake has two similar variables to set -- SFMLDIR and SFML_DIR. SFML_DIR keeps getting reset to have the value of SFML_DIR-NOTFOUND but SFMLDIR doesn't. I just checked out the latest version of thor from trunk. I get this error no matter what I do:

CMake Warning at CMakeLists.txt:86 (find_package):
  Could not find module FindSFML.cmake or a configuration file for package
  SFML.

  Adjust CMAKE_MODULE_PATH to find FindSFML.cmake or set SFML_DIR to the
  directory containing a CMake configuration file for SFML.  The file will
  have one of the following names:

    SFMLConfig.cmake
    sfml-config.cmake



CMake Error at CMakeLists.txt:92 (message):
  SFML directory not found.  Set SFMLDIR to SFML's top-level path (containing
  "include" and "lib" directories).

4
Graphics / sf::Image deprecated? sf::Texture? a problem with rendering
« on: January 01, 2012, 12:19:19 am »
Haven't used sfml in a while, came back to start a new project and am trying to figure stuff out. I used to load something in a sf::Image, and then assign it to a sf::Sprite. Now it seems that sf::Texture has taken its place? Just making sure they're the same thing cause I'm having a problem with getting stuff to draw properly. Looks like corrupted memory somewhere when I render.


And am I doing this right? Loading tiles from a tileset (larger image with all my tiles). I want to make sure the sprite only has that subrect of the image stored in it, or a lot of memory would be taken up.

sprite->SetTexture(*texture);
sprite->SetTextureRect(sf::IntRect(sourceX, sourceY, tileset->GetTileWidth(), tileset->GetTileHeight()));



Might anyone know why this doesn't render properly? If I take out the sprite->SetTextureRect line, the whole tileset renders properly just fine.

Code: [Select]

bool Map::LoadMap(std::string filename)
{
Tmx::Map *map = new Tmx::Map();
map->ParseFile(filename);

if (map->HasError()) {
printf("error code: %d\n", map->GetErrorCode());
printf("error text: %s\n", map->GetErrorText().c_str());

return map->GetErrorCode();
}

for(int i = 0; i < map->GetNumTilesets(); i++)
{
const Tmx::Tileset* tileset = map->GetTileset(i);

sf::Texture* texture = new sf::Texture();


if(!texture->LoadFromFile(tileset->GetImage()->GetSource().c_str()))
{
return false;
}

for(int y = 0; y < (tileset->GetImage()->GetWidth() / tileset->GetTileWidth()); y++)
{
for(int x = 0; x < (tileset->GetImage()->GetHeight() / tileset->GetTileHeight()); x++)
{
int margin = tileset->GetMargin();
int spacing = tileset->GetSpacing();
int sourceX = (margin + (x * tileset->GetTileWidth()) + (inRange(x, 1, (tileset->GetTileWidth() * (tileset->GetImage()->GetWidth() - tileset->GetTileWidth()))) ? (x * spacing) : 0));
int sourceY = (margin + (y * tileset->GetTileHeight()) + (inRange(y, 1, (tileset->GetTileHeight() * (tileset->GetImage()->GetHeight() - tileset->GetTileHeight()))) ? (y * spacing) : 0));
int sourceWidth = sourceX + tileset->GetTileWidth();
int sourceHeight = sourceY + tileset->GetTileHeight();

sf::Sprite* sprite = new sf::Sprite();

sprite->SetTexture(*texture);
sprite->SetTextureRect(sf::IntRect(sourceX, sourceY, tileset->GetTileWidth(), tileset->GetTileHeight()));

sf::Texture t;

unsigned int gid = tileset->GetFirstGid() + x * y;
_tileSprites[gid] = sprite;

std::cout << gid << " ";

}
}
}

for (int i = 0; i < map->GetNumLayers(); ++i)
{
const Tmx::Layer* layer = map->GetLayer(i);

for (int y = 0; y < layer->GetHeight(); y++)
{
for (int x = 0; x < layer->GetWidth(); x++)
{
// Find a tileset for that id.
unsigned int tileGID = layer->GetTileGid(y,x);

if(tileGID != 0)
{
_tiles.push_back(new Tile(tileGID, sf::Vector2f(x * 32, y * 32)));

//printf("Tile position on layer: x=%d, y=%d\n", x * map->GetTileWidth(), y * map->GetTileHeight());
//printf("Tile rect on tileset: x=%d, y=%d, w=%d, h=%d\n", sourceX, sourceY, sourceWidth, sourceHeight);
//printf("Tile Size: w=%d, h=%d\n", tileset->GetTileWidth(), tileset->GetTileHeight());
//printf("Tile Source: %s\n", tileset->GetImage()->GetSource().c_str());
//printf("\n");

}
else
{
// no tileset found. blank tile.
}
}
}
}

delete map;
}


Code: [Select]

while(_renderer->GetRenderWindow()->IsOpened())
{
sf::Event Event;
        while (_renderer->GetRenderWindow()->PollEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                _renderer->GetRenderWindow()->Close();
        }

        // Clear screen
        _renderer->GetRenderWindow()->Clear();

for(int i = 0; i < m->_tiles.size(); i++)
{
Tile* t = m->_tiles.at(i);
sf::Sprite* s = m->_tileSprites[t->GetGID()];
if(s)
{
std::cout << s << ": " << t->GetPosition().x << "," << t->GetPosition().y << std::endl;
s->SetPosition(t->GetPosition());
_renderer->GetRenderWindow()->Draw(*s);
}
}

        // Finally, display the rendered frame on screen
        _renderer->GetRenderWindow()->Display();
}



Here's without the subrect line: http://imgbin.org/index.php?page=image&id=6140

Here's with the subrect line: http://imgbin.org/index.php?page=image&id=6141

Thanks.

5
Graphics / Possible weird bug (or user error :D)
« on: May 03, 2011, 10:32:59 am »
Hey all,

I'm using sfml along with box2d for a project I'm working on. I implemented box2d's DebugDraw class and try to use it but there's some weird caveat to it I'm not sure about. If I don't call sfml's Draw() method (eg the third line, _window->Draw()), nothing at all draws. I don't see the debug data drawn and I don't understand why. However, when I have that line there, the circle shape draws fine, along with the debug data on top of it. DrawDebugData uses raw opengl calls to draw everything. Is there something I'm not doing, like swapping the buffers or something? I thought that was essentially what Display() did.

Code: [Select]

_window->Clear(sf::Color::Black);
_world->DrawDebugData();
_window->Draw(circle);
_window->Display();


I mean, I'm pretty sure I'll always be drawing something along with the debug data. It'd just be nice to know why this is and what I'm doing wrong, if anything. My friend and I were sitting there for over an hour trying to figure out why the debug data wasn't drawing, and then I decided to draw a sf::Shape just to test it out and that made the debug data show up all of a sudden.

Thanks!

6
General discussions / SFML 2.0 with opengl
« on: April 10, 2011, 07:40:35 am »
Might I ask if you got this working? I'm trying to do the same thing, yet none of the opengl things are working. From the tutorials:

Quote
To use OpenGL, you only have to include Window.hpp : the OpenGL and GLU headers will be automatically included by it. This is to prevent you from having to use preprocessor, as OpenGL headers have different names on each operating system."


So I tried that, yet I still get a ton of errors saying things like:

Quote
1>DebugDraw.cpp(29): error C3861: 'glBegin': identifier not found


What am I doing wrong?

Code: [Select]
#ifndef DEBUG_DRAW_H
#define DEBUG_DRAW_H

#include <Box2D/Box2D.h>

#include <SFML/Window.hpp>

struct b2AABB;

// This class implements debug drawing callbacks that are invoked
// inside b2World::Step.
class DebugDraw : public b2DebugDraw
{
public:
void DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color);

void DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color);

void DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color);

void DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color);

void DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color);

void DrawTransform(const b2Transform& xf);

    void DrawPoint(const b2Vec2& p, float32 size, const b2Color& color);

    void DrawString(int x, int y, const char* string, ...);

    void DrawAABB(b2AABB* aabb, const b2Color& color);
};


#endif


Code: [Select]

/*
* Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
*
* This software is provided 'as-is', without any express or implied
* warranty.  In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/

#include "DebugDraw.h"

#include <cstdio>
#include <cstdarg>

#include <cstring>

void DebugDraw::DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
{
glColor3f(color.r, color.g, color.b);
glBegin(GL_LINE_LOOP);
for (int32 i = 0; i < vertexCount; ++i)
{
glVertex2f(vertices[i].x, vertices[i].y);
}
glEnd();
}

void DebugDraw::DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
{
glEnable(GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4f(0.5f * color.r, 0.5f * color.g, 0.5f * color.b, 0.5f);
glBegin(GL_TRIANGLE_FAN);
for (int32 i = 0; i < vertexCount; ++i)
{
glVertex2f(vertices[i].x, vertices[i].y);
}
glEnd();
glDisable(GL_BLEND);

glColor4f(color.r, color.g, color.b, 1.0f);
glBegin(GL_LINE_LOOP);
for (int32 i = 0; i < vertexCount; ++i)
{
glVertex2f(vertices[i].x, vertices[i].y);
}
glEnd();
}

void DebugDraw::DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color)
{
const float32 k_segments = 16.0f;
const float32 k_increment = 2.0f * b2_pi / k_segments;
float32 theta = 0.0f;
glColor3f(color.r, color.g, color.b);
glBegin(GL_LINE_LOOP);
for (int32 i = 0; i < k_segments; ++i)
{
b2Vec2 v = center + radius * b2Vec2(cosf(theta), sinf(theta));
glVertex2f(v.x, v.y);
theta += k_increment;
}
glEnd();
}

void DebugDraw::DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color)
{
const float32 k_segments = 16.0f;
const float32 k_increment = 2.0f * b2_pi / k_segments;
float32 theta = 0.0f;
glEnable(GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4f(0.5f * color.r, 0.5f * color.g, 0.5f * color.b, 0.5f);
glBegin(GL_TRIANGLE_FAN);
for (int32 i = 0; i < k_segments; ++i)
{
b2Vec2 v = center + radius * b2Vec2(cosf(theta), sinf(theta));
glVertex2f(v.x, v.y);
theta += k_increment;
}
glEnd();
glDisable(GL_BLEND);

theta = 0.0f;
glColor4f(color.r, color.g, color.b, 1.0f);
glBegin(GL_LINE_LOOP);
for (int32 i = 0; i < k_segments; ++i)
{
b2Vec2 v = center + radius * b2Vec2(cosf(theta), sinf(theta));
glVertex2f(v.x, v.y);
theta += k_increment;
}
glEnd();

b2Vec2 p = center + radius * axis;
glBegin(GL_LINES);
glVertex2f(center.x, center.y);
glVertex2f(p.x, p.y);
glEnd();
}

void DebugDraw::DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color)
{
glColor3f(color.r, color.g, color.b);
glBegin(GL_LINES);
glVertex2f(p1.x, p1.y);
glVertex2f(p2.x, p2.y);
glEnd();
}

void DebugDraw::DrawTransform(const b2Transform& xf)
{
b2Vec2 p1 = xf.position, p2;
const float32 k_axisScale = 0.4f;
glBegin(GL_LINES);

glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(p1.x, p1.y);
p2 = p1 + k_axisScale * xf.R.col1;
glVertex2f(p2.x, p2.y);

glColor3f(0.0f, 1.0f, 0.0f);
glVertex2f(p1.x, p1.y);
p2 = p1 + k_axisScale * xf.R.col2;
glVertex2f(p2.x, p2.y);

glEnd();
}

void DebugDraw::DrawPoint(const b2Vec2& p, float32 size, const b2Color& color)
{
glPointSize(size);
glBegin(GL_POINTS);
glColor3f(color.r, color.g, color.b);
glVertex2f(p.x, p.y);
glEnd();
glPointSize(1.0f);
}

void DebugDraw::DrawString(int x, int y, const char *string, ...)
{
char buffer[128];

va_list arg;
va_start(arg, string);
vsprintf(buffer, string, arg);
va_end(arg);

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
int w = glutGet(GLUT_WINDOW_WIDTH);
int h = glutGet(GLUT_WINDOW_HEIGHT);
gluOrtho2D(0, w, h, 0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

glColor3f(0.9f, 0.6f, 0.6f);
glRasterPos2i(x, y);
int32 length = (int32)strlen(buffer);
for (int32 i = 0; i < length; ++i)
{
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, buffer[i]);
}

glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}

void DebugDraw::DrawAABB(b2AABB* aabb, const b2Color& c)
{
glColor3f(c.r, c.g, c.b);
glBegin(GL_LINE_LOOP);
glVertex2f(aabb->lowerBound.x, aabb->lowerBound.y);
glVertex2f(aabb->upperBound.x, aabb->lowerBound.y);
glVertex2f(aabb->upperBound.x, aabb->upperBound.y);
glVertex2f(aabb->lowerBound.x, aabb->upperBound.y);
glEnd();
}


Thanks.

edit: Alright, mostly fixed it. Found a post where Laurent said opengl is no longer included in Window, so I needed to include SFML/OpenGL.hpp.

Now I just need to find a solution to using
glutGet(GLUT_WINDOW_WIDTH);
and
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, buffer);

since glut isn't a part of SFML... any ideas on what I can use instead? for now I'll just comment out drawstring like he did above since I won't need it for now.

Now I'm getting:

1>DebugDraw.obj : error LNK2019: unresolved external symbol __imp_glEnd referenced in function "public: virtual void __cdecl DebugDraw::DrawPolygon(struct b2Vec2 const *,int,struct b2Color const &)" (?DrawPolygon@DebugDraw@@UEAAXPEBUb2Vec2@@HAEBUb2Color@@@Z)
1>DebugDraw.obj : error LNK2019: unresolved external symbol __imp_glVertex2f referenced in function "public: virtual void __cdecl DebugDraw::DrawPolygon(struct b2Vec2 const *,int,struct b2Color const &)" (?DrawPolygon@DebugDraw@@UEAAXPEBUb2Vec2@@HAEBUb2Color@@@Z)
1>DebugDraw.obj : error LNK2019: unresolved external symbol __imp_glBegin referenced in function "public: virtual void __cdecl DebugDraw::DrawPolygon(struct b2Vec2 const *,int,struct b2Color const &)" (?DrawPolygon@DebugDraw@@UEAAXPEBUb2Vec2@@HAEBUb2Color@@@Z)
1>DebugDraw.obj : error LNK2019: unresolved external symbol __imp_glColor3f referenced in function "public: virtual void __cdecl DebugDraw::DrawPolygon(struct b2Vec2 const *,int,struct b2Color const &)" (?DrawPolygon@DebugDraw@@UEAAXPEBUb2Vec2@@HAEBUb2Color@@@Z)
1>DebugDraw.obj : error LNK2019: unresolved external symbol __imp_glDisable referenced in function "public: virtual void __cdecl DebugDraw::DrawSolidPolygon(struct b2Vec2 const *,int,struct b2Color const &)" (?DrawSolidPolygon@DebugDraw@@UEAAXPEBUb2Vec2@@HAEBUb2Color@@@Z)
1>DebugDraw.obj : error LNK2019: unresolved external symbol __imp_glColor4f referenced in function "public: virtual void __cdecl DebugDraw::DrawSolidPolygon(struct b2Vec2 const *,int,struct b2Color const &)" (?DrawSolidPolygon@DebugDraw@@UEAAXPEBUb2Vec2@@HAEBUb2Color@@@Z)
1>DebugDraw.obj : error LNK2019: unresolved external symbol __imp_glBlendFunc referenced in function "public: virtual void __cdecl DebugDraw::DrawSolidPolygon(struct b2Vec2 const *,int,struct b2Color const &)" (?DrawSolidPolygon@DebugDraw@@UEAAXPEBUb2Vec2@@HAEBUb2Color@@@Z)
1>DebugDraw.obj : error LNK2019: unresolved external symbol __imp_glEnable referenced in function "public: virtual void __cdecl DebugDraw::DrawSolidPolygon(struct b2Vec2 const *,int,struct b2Color const &)" (?DrawSolidPolygon@DebugDraw@@UEAAXPEBUb2Vec2@@HAEBUb2Color@@@Z)
1>DebugDraw.obj : error LNK2019: unresolved external symbol __imp_glPointSize referenced in function "public: void __cdecl DebugDraw::DrawPoint(struct b2Vec2 const &,float,struct b2Color const &)" (?DrawPoint@DebugDraw@@QEAAXAEBUb2Vec2@@MAEBUb2Color@@@Z)


Anyone know how to fix this?

another edit: fixed it by doing:
#pragma comment(lib, "opengl32.lib")

Seems like a messy way of fixing it... but it worked. Is that the best way?

7
Graphics / With an Image, open new Form
« on: February 08, 2011, 04:07:44 am »
If I understand what you're asking, you'll need to implement some collision detection with the mouse and the "Start" image. You could use a basic point in rect test.

Code: [Select]

if(mouseX > startImgX &&
   mouseX < startImgX + startImgWidth &&
   mouseY > startImgY &&
   mouseY < startImgY + startImgHeight)
{
     race.run();
}


Think that logic is right. Might have screwed it up, but it should give you the general idea. :)

8
General discussions / Problem with crashing on exit
« on: February 07, 2011, 10:04:06 pm »
Oooh very nice. I did what JAssange said and it fixed it. Should do for now!

Thanks... I swear I searched around a little before posting. Guess I just didn't look hard enough.

9
General discussions / Problem with crashing on exit
« on: February 07, 2011, 09:06:48 am »
Radeon HD 4870 using an older version of catalyst version 10.8 since the sfml window wouldn't show up using the latest version of it.

10
General discussions / Problem with crashing on exit
« on: February 07, 2011, 07:09:09 am »
Probably something stupid I did, but whenever I exit my program I get:

The thread 'Win64 Thread' (0x12a0) has exited with code 0 (0x0).
First-chance exception at 0x690adf99 in isometric.exe: 0xC0000005: Access violation reading location 0x0000000000000010.
Unhandled exception at 0x690adf99 in isometric.exe: 0xC0000005: Access violation reading location 0x0000000000000010.

and crt0dat.c pops open and points to this block of code:

Code: [Select]

void __cdecl __crtExitProcess (
        int status
        )
{
        __crtCorExitProcess(status);

        /*
         * Either mscoree.dll isn't loaded,
         * or CorExitProcess isn't exported from mscoree.dll,
         * or CorExitProcess returned (should never happen).
         * Just call ExitProcess.
         */

        ExitProcess(status);
}


Any idea what might cause this?

11
General / SFML does not show windows on x64
« on: February 06, 2011, 11:50:09 pm »
Uninstalled old catalyst stuff and installed 10.8 and it fixed it! Thank you so much! Been tearing my hair out over this for a while now. Finally get to get going on this!

So does this mean if I distribute software I make with sfml2, people won't be able to run it unless they're using catalyst <= 10.8? Or is this an issue with debug only?

12
General / SFML does not show windows on x64
« on: February 06, 2011, 10:12:30 pm »
I'm having the same problem after compiling sfml 2.0 on x64. Was thinking it was something I did wrong related to compiling/linking or maybe I didn't have the right x64 extlibs.

I also have a radeon hd 4870. Just tried installing the latest catalyst version and it didn't fix it.

Agh so desperate to start making something with sfml but I keep running into problems getting it setup. :(

Pages: [1]
anything