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 - raycrasher

Pages: [1] 2
1
SFML projects / Re: Cloud Wars - WIP
« on: July 18, 2013, 01:29:51 pm »
Yeah, it's basically a clone of Battleships Forever. I even asked permission on the BSF forum for use of the sprites. They didn't seem to mind. :) I've been playing BSF for a long time, and I thought of making a game like that just for kicks. And add a little spice to it while I'm at it.

However, this is made in C# and SFML 2.0, so its a LOT faster, especially when lots of ships and bullets are flying. And it has C# scripting support, for the modders.


2
SFML projects / Re: Cloud Wars - WIP
« on: July 17, 2013, 10:23:25 am »
After one year...

AN UPDATE! Yay!

It has been almost year since the last post, but the game has had significant developments. Specifically, I grew tired of the compile times I am getting with C++ (I use Boost!). So I switched to C#, and used a custom C++/CLI wrapper around the C++ SFML libraries to improve performance and get around the glitches I was getting with the .NET version of SFML. The game is not yet finished, but I am really planning on finishing this before March of next year, especially because this is my thesis project.

Here is the result so far:







3
Graphics / Re: Are off-screen objects rendered?
« on: July 08, 2013, 10:55:11 am »
I wonder - if I have a large worldspace and have lots of things to draw scattered on that world, should it take less time to check each object if it is actually visible (AABB intersection with appropriate transforms) before drawing than to just draw it all?

4
Graphics / Re: Complex Map Generation & Rendering
« on: July 08, 2013, 10:48:47 am »
Just use a parallax value for each game object, and use it to calculate that object's final transform to be passed in a RenderState.

Here's a snippet from a C# project of mine. Position and Zoom are camera position and zoom. xform is the transform adjusted for parallax. Multiply your object's world transform by it and you get your object's final transform, adjusted for both position and zoom parallax.

            Transform xform = Transform.Identity;
            float scaleFactor = 1/(1 + parallax*Zoom)*2;

            xform.Scale(scaleFactor, scaleFactor);
            xform.Translate(-Position*parallax);
 


5
Graphics / Re: How to draw masked / stenciled graphics?
« on: July 08, 2013, 10:21:05 am »
You're right, getting stencil to work properly with SFML is hard. I ended up using alternate sprites, which is way easier and better looking.

I used this as reference:
http://en.sfml-dev.org/forums/index.php?topic=3108.0

It didn't work for me, since I was trying to implement it in C# :D But it would be a nice add-on to SFML if it did work.

Anyway, thanks!

6
General / Re: SFML game with LibRocket?
« on: June 20, 2013, 06:13:22 am »
I have tried using it with SFML 2.0 and SFML.NET, and I had very good results so far. You don't have to go far in order to make it compatible with SFML. For the renderer, just use the sample OpenGL renderer.

7
Graphics / Re: Setting origin of View
« on: June 20, 2013, 05:57:21 am »
A good approach is not to use View as the basis for your camera class. Instead, use an sf::Transform wrapped in a Camera class, and pass that transform inside the RenderStates parameter when drawing.

8
Graphics / How to draw masked / stenciled graphics?
« on: June 20, 2013, 05:31:28 am »
In my project I have to draw damage sprites (hull breaches, smoldering twisted metal, etc.) over ships depending on where they are hit. The problem is, I want those damage sprites to be drawn only over non-transparent parts of the ship, because drawing a burning piece of bulkhead over empty space looks plain weird.  So I'm asking for help on how should I do this.

Stencil masks? Render to texture? Shaders?

Currently, I'm thinking along the lines of the following:

1. Draw hull(s) normally, but also draw to stencil all pixels which has alpha==255.
2. Draw damage sprites, using stencil mask.
3. Reset stencil so that subsequent draws will not be affected.

But how, OpenGL and code wise? Many thanks in advance!

9
DotNet / Re: c# Sfml 2.0 GUI
« on: May 14, 2013, 07:30:24 am »
You can try wrapping libRocket in a C++/CLI wrapper, and use the resulting class library for .NET. I created one, and it works beautifully.

10
DotNet / Why P/Invoke?
« on: May 14, 2013, 07:28:12 am »
I'm just wondering why SFML.NET uses P/Invoke and not a C++/CLI wrapper?

I have read that a custom C++/CLI wrapper gives better performance that using P/Invoke : manual marshalling, coarse interfaces, etc. Especially when using SFML.NET, since there's an additional layer involved (CSFML). I had previous experience wrapping up the libRocket GUI library for use in .NET, and it works great, so it should work for SFML.

Also, a little background: I have been getting weird errors when using the latest version of the .NET binding, so as a result, I'm creating my own C++/CLI wrapper for use in C#. That took care of the errors, and it runs great, but it still needs some work.

11
The 2.0 RC version of SFML downloaded from the main site works for me. I've circumvented the problem by using the 2.0 RC version and adding the required features as C# extensions to the library.

Its the latest commit (86897a8347) that doesn't work for me.

I would like to know what the commit hash for the source of SFML2.NET RC, so that I could try comparing the two versions and find out what went wrong.

12
By the way, I'm using Visual Studio 2012.

I also tried recompiling sfml, csfml, and sfml.net from the latest sources, the error still persists. Weird though - all the samples work, though the error still shows up.

Also debugged the source last night, a WindowImplWin32 object was only created when the program quits. Is this correct behavior?

I'm going to try modifying the RC in order to get the features I want, nothing else. If this fails, then maybe its because of VS2012 and not SFML.

13
Code: [Select]
Failed to set pixel format for device context -- cannot create OpenGL context
SFML.NET (latest version from GitHub) gives this error when creating a RenderWindow. Sample code not given, since the same error occurs with the OpenGL samples (be sure to enable the console window). I would be content to just ignore the error if SFML works regardless, but my program, which worked fine with SFML.NET RC downloaded from the main SFML site, doesn't display fonts/renders sprites as white rectangles with the Github version. I need the latest version in order to use the new Draw overloads for RenderWindow.


OS: Windows 8
Video: Nvidia GeForce GT 220 (driver version 306.14).

I'm currently updating my video card drivers just in case.

Help!

14
SFML wiki / Sprite batching that works with multiple textures
« on: February 10, 2013, 09:29:13 am »
Here's a SpriteBatch class I created that allows you to just put in sf::Sprites without regard for texture. The class automatically constructs sf::VertexArrays for consecutive sprites with the same texture, blend mode, and shader. It's derived from sf::Drawable and sf::Transformable, so you can use it much like a sprite.

Tell me what you think. I'm sure this class still has much room for improvement. Also, feel free to use and modify it for your projects.

EDIT: I haven't posted this to the wiki yet, but I will post if this is useful.

To use:

sf::Sprite spr;

...

SpriteBatch batch;
SpriteBatchItem theSubItem = batch.AddItem(spr); // add a sprite. Differing textures are automatically handled.
SpriteBatchItem theSubItem2 = batch.AddItem(spr, sf::BlendAdd, myShader); // specify different blend mode and shader (this creates a different batch if the previous add is different in texture, blend mode or shader)

theSubItem.setPosition(50.f,50.f); // you can use sf::Transformable methods
theSubItem.setColor(sf::Color::White); // you can set the color of all 4 vertices
theSubItem.setColor(sf::Color::Red,2); // you can set the color of the third vertex (index 2)
theSubItem.UpdateSpriteBatch(); // update the sprite batch (update only if the sprite batch item is transformed

window.draw(batch); // draw the batch

batch.RemoveItem(theSubItem2); // remove a batch item
 

Requires: weak_ptr, shared_ptr, C++11

SpriteBatch.h
#pragma once

#include <list>

class SpriteBatchItem;

class SpriteBatch: public sf::Drawable, public sf::Transformable {
   friend class SpriteBatchItem;

   struct Batch {
      sf::RenderStates states;
      std::vector<sf::Vertex> vertices;
   };

   struct Quad {
      std::list<Batch>::iterator batch;
      std::list<shared_ptr<Quad>>::iterator quadIter;
      int vertexIndex;
      sf::IntRect texCoords;
      SpriteBatch *spriteBatch;
   };



public:

   SpriteBatch();
   ~SpriteBatch();

   SpriteBatchItem AddItem(const sf::Sprite &spr, sf::BlendMode blendMode=sf::BlendAlpha, sf::Shader *shader=nullptr);
   void RemoveItem(SpriteBatchItem &item);
   sf::FloatRect GetLocalBounds();
   sf::FloatRect GetGlobalBounds();
   void Clear();

protected:
   void draw(sf::RenderTarget &target, sf::RenderStates states) const;
private:
   
   sf::FloatRect bounds;
   bool boundsInvalidated;
   std::list<shared_ptr<Quad>> quads;
   std::list<Batch> batches;
};

class SpriteBatchItem: public sf::Transformable {
   friend class SpriteBatch;
public:
   void SetColor(const sf::Color &c, unsigned vtx=4);
   sf::Color GetColor(unsigned vtx=0);
   bool IsValid();
   void UpdateSpriteBatch();
   SpriteBatch &GetSpriteBatch();

   sf::FloatRect GetLocalBounds() const;
   sf::FloatRect GetGlobalBounds() const;

private:
   weak_ptr<SpriteBatch::Quad> quad;
};
 

SpriteBatch.cpp
#include "SpriteBatch.h"

SpriteBatch::SpriteBatch(){}
SpriteBatch::~SpriteBatch(){}

SpriteBatchItem SpriteBatch::AddItem(const sf::Sprite &spr, sf::BlendMode blendMode, sf::Shader *shader) {
   if(batches.empty() || (spr.getTexture() != batches.back().states.texture) || blendMode != batches.back().states.blendMode || shader != batches.back().states.shader) {
      batches.push_back(Batch());
      batches.back().states.texture=spr.getTexture();
      batches.back().states.shader=shader;
      batches.back().states.blendMode=blendMode;
   }

   shared_ptr<Quad> q(new Quad);
   q->batch = (--batches.end());
   q->spriteBatch = this;
   q->vertexIndex = batches.back().vertices.size();
   q->texCoords = spr.getTextureRect();

   batches.back().vertices.resize(batches.back().vertices.size()+4);
   q->batch->vertices[q->vertexIndex].texCoords=sf::Vector2f((float)q->texCoords.left,(float)q->texCoords.top);
   q->batch->vertices[q->vertexIndex+1].texCoords=sf::Vector2f((float)q->texCoords.left,(float)q->texCoords.top+q->texCoords.height);
   q->batch->vertices[q->vertexIndex+2].texCoords=sf::Vector2f((float)q->texCoords.left+q->texCoords.width,(float)q->texCoords.top+q->texCoords.height);
   q->batch->vertices[q->vertexIndex+3].texCoords=sf::Vector2f((float)q->texCoords.left+q->texCoords.width,(float)q->texCoords.top);
   
   

   quads.push_back(q);
   q->quadIter=(--quads.end());

   

   static SpriteBatchItem sbi;
   sbi.quad = q;
   sbi.SetColor(spr.getColor());
   sbi.setOrigin(spr.getOrigin());
   sbi.setPosition(spr.getPosition());
   sbi.setScale(spr.getScale());
   sbi.UpdateSpriteBatch();
   return sbi;
}

void SpriteBatch::RemoveItem(SpriteBatchItem &item) {
   auto q=item.quad.lock();
   if(q && q->spriteBatch==this){
      auto start=q->quadIter;
      start++;
      q->batch->vertices.erase(q->batch->vertices.begin()+q->vertexIndex,q->batch->vertices.begin()+q->vertexIndex+4);
      for(auto iter=start;iter!=quads.end();iter++){
         if(q->batch == (*iter)->batch) {
            (*iter)->vertexIndex-=4;
         }
      }
      if(q->batch->vertices.size()==0) batches.erase(q->batch);
      quads.erase(q->quadIter);
   }
}

void SpriteBatch::draw(sf::RenderTarget &target, sf::RenderStates states) const {
   states.transform*=getTransform();
   for(auto &b : batches){
      states.texture=b.states.texture;
      states.shader=b.states.shader;
      states.blendMode=b.states.blendMode;
      target.draw(&b.vertices[0],b.vertices.size(),sf::Quads,states);
   }
}

void SpriteBatch::Clear() {
   quads.clear();
   batches.clear();
}


void SpriteBatchItem::UpdateSpriteBatch() {
   auto q=quad.lock();
   if(q){
      sf::Vertex *vtx[4] = {
         &q->batch->vertices[q->vertexIndex],
         &q->batch->vertices[q->vertexIndex+1],
         &q->batch->vertices[q->vertexIndex+2],
         &q->batch->vertices[q->vertexIndex+3]
      };

      vtx[0]->position = getTransform().transformPoint(sf::Vector2f(0.f, 0.f));
      vtx[1]->position = getTransform().transformPoint(sf::Vector2f(0.f, (float)q->texCoords.height));
      vtx[2]->position = getTransform().transformPoint(sf::Vector2f((float)q->texCoords.width, (float)q->texCoords.height));
      vtx[3]->position = getTransform().transformPoint(sf::Vector2f((float)q->texCoords.width, 0.f));
   }
}

void SpriteBatchItem::SetColor( const sf::Color &c, unsigned vtx ) {
   auto q=quad.lock();
   if(q){
      if(vtx >= 4){
         q->batch->vertices[q->vertexIndex].color=c;
         q->batch->vertices[q->vertexIndex+1].color=c;
         q->batch->vertices[q->vertexIndex+2].color=c;
         q->batch->vertices[q->vertexIndex+3].color=c;
      } else {
         q->batch->vertices[q->vertexIndex+vtx].color=c;
      }
   }
}

sf::Color SpriteBatchItem::GetColor(unsigned vtx) {
   auto q=quad.lock();
   if(q){
      if(vtx >= 4) vtx=0;
      return q->batch->vertices[q->vertexIndex+vtx].color;
   }
   else return sf::Color(0.f,0.f,0.f,0.f);
}

bool SpriteBatchItem::IsValid() {
   return ((bool) quad.lock());
}

SpriteBatch &SpriteBatchItem::GetSpriteBatch() {
   return *quad.lock()->spriteBatch;
}

sf::FloatRect SpriteBatch::GetLocalBounds()
{
   if(boundsInvalidated)
   {
      sf::Vector2f topleft(0.f,0.f),bottomRight(0.f,0.f);

      for(auto &b : batches){
         for(auto &v : b.vertices){
            topleft.x = std::min(topleft.x,v.position.x);
            topleft.y = std::min(topleft.y,v.position.y);
            bottomRight.x=std::max(bottomRight.x,v.position.x);
            bottomRight.y=std::max(bottomRight.y,v.position.y);
         }
      }
      bounds=sf::FloatRect (topleft,bottomRight-topleft);
      boundsInvalidated=false;
   }
   
   return bounds;
}

sf::FloatRect SpriteBatch::GetGlobalBounds() {
   return getTransform().transformRect(GetLocalBounds());
}

sf::FloatRect SpriteBatchItem::GetLocalBounds() const
{
   auto q=quad.lock();
   if(q){
      return sf::FloatRect(0.f, 0.f, (float) q->texCoords.width, (float) q->texCoords.height);
   }
   else return sf::FloatRect();
}


////////////////////////////////////////////////////////////
sf::FloatRect SpriteBatchItem::GetGlobalBounds() const
{
   return getTransform().transformRect(GetLocalBounds());
}
 

15
SFML projects / Re: Cloud Wars - WIP
« on: July 21, 2012, 11:04:52 am »
Thanks for the replies! :)

Anyway, I just finished the steering behaviors the other day, and the ships are now chasing each other all over the screen. I went with the simple solution, which is to apply the desired vector as a force somewhere near the ship's nose, increase angular damping to about 0.8, and multiply the angular rotation by 0.95 when the heading of the desired vector is less than 30 degrees. Its an ugly hack, but it gives good results. I'm going to simulate thrusters firing with data retrieved from b2Body::GetLinearVelocityFromLocalPoint.

In the works - using Lua as a scripting language for RML (Rocket Markup Language, an HTML/CSS-like language for the GUI), should be simple enough, but there's much work to be done refactoring the mess inside ShipEditor. When it's done, I can then use Lua inside RML element event handlers, like:
<button id="myButton" onclick="Gui.Document('MainMenu'):GetElementByID('myButton'):SetInnerRML('You clicked me')">Click</button>

Pages: [1] 2
anything