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

Pages: [1]
1
Graphics / [Solved] Text Display Problem
« on: January 27, 2012, 06:55:41 pm »
And, as always, it's a simple matter of deep-testing and reorganization that saved the day!

I made a barebones version of what I want based on this thread:

Code: [Select]

#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <vector>
#include <string>

using std::vector;
using std::string;

#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <sstream>

int main()
{
    // Create the main window
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
    sf::RenderTexture Texture;
      Texture.Create(300, 100);
      Texture.Clear();
   

    // Create a graphical text to display
    sf::Text text("");

    sf::Clock timer;
    unsigned int character = 0; float x = 0, y = 0;
    std::string str = "... someone hates me !";

    // Start the game loop
    sf::Texture buff = Texture.GetTexture();
    sf::Sprite sprite;
    sprite.SetPosition(0, 0);
    sprite.SetTexture(buff);

    while (window.IsOpened()) {
        // Process events
        sf::Event event;
        while (window.PollEvent(event))   {
            // Close window : exit
            if (event.Type == sf::Event::Closed || (event.Type == sf::Event::KeyPressed && event.Key.Code == sf::Keyboard::Escape))
            window.Close();
        }

        if (timer.GetElapsedTime() > 150 && character < str.size())  {
            timer.Reset();
            text.SetPosition(x, y);
            text.SetString(sf::String(str[character]));
            character++;
            x += text.GetRect().Width;  
        }
         
        buff = Texture.GetTexture();
        // Draw the string
        Texture.Draw(text);
        Texture.Display();
        window.Clear();
        // Clear screen
        window.Draw(sprite);

        // Update the window
        window.Display();
    }

    return EXIT_SUCCESS;
}


Based on the fact that this worked perfectly, I determined one part of the problem was that the RenderTexture was being displayed and drawn in the wrong order. That fixed the size issue, but the letters were still jumbled.

I took an older Message structure that I had made in the Allegro days and tweaked it to work with SFML, and now the text displays perfectly. :)

2
Graphics / [Solved] Text Display Problem
« on: January 27, 2012, 03:34:20 am »
I've also had an Image copy the printed data and save it to an image. This is what comes up the first time I open the message:



All subsequent tries show the same, but in an even smaller font that's indecipherable.

For what's it worth, the message should be "Oh, for heaven's sake! Why will this not work?"

I hope that helps if anyone has any idea what the problem could be.

3
Graphics / [Solved] Text Display Problem
« on: January 26, 2012, 09:19:36 pm »
Having done some testing, I narrowed the problem down some--I copied the Display object to an Image and saved it  as a file. The file shows the letters perfectly displayed at the right size, but when the Display is drawn to the buffer, it shows mangled characters after the first few, and repeated attempts only show the first letter.

Could them both being both RenderTextures be the problem? I haven't  seen much around here about drawing between RenderTextures--is it impractical/unstable?

4
Graphics / [Theory] Sprite Layering
« on: January 26, 2012, 03:59:46 pm »
Thanks. I will post if I can think of a more efficient method. It does work, but so far the only other thing that comes to mind is changing the container from a map to a multimap--that would eliminate the need for a check of the y-coordinates since mutlimaps can have multiple elements with the same key.

5
Graphics / [Solved] Text Display Problem
« on: January 26, 2012, 03:54:25 pm »
I'm testing displaying text in sfml 2.0, "typewriter" style. The way it's working now, it works properly for the first seven characters, then it all goes to hell. Even weirder, the text gets smaller every time I access the message. I don't know what I'm doing wrong here!

Here's some relevant code--if more is needed, let me know.

Message structure:
Code: [Select]

void Message::Create(string file, string key, float xx, float yy, float ww, float hh, float sz) {
    string temp;
  ifstream istream(file);
 
  //until the stream finds the search term to search in the text document, it will iterate through it.
  for(file; temp.find(key) == -1; getline(istream, temp));
  for(file; temp != "[end]"; text += temp, getline(istream, temp));

  x = y = 0;
  w = ww; h = hh; size = sz;

  pos = 0;;
  pause = done;
  print = undone;

//Display is a Canvas structure which contains an sf::RenderTexture (buff) among other values
  Display.Make(w, h);
  Display.Set(xx, yy);
  Display.buff.Clear(BLACK);
 
//Data is sf::Text
  Data.SetString(text.at(pos));
  Data.SetPosition(0, 0);
  Data.SetCharacterSize(size);

  istream.close();
}

 void Message::Logic() {
  if (print) return;
  ++pos;
  if (pos == text.length() - 1) {
    print = done;
    return;
  }
 
  x + Data.GetRect().Width > w? x = 0, y += Data.GetRect().Height:  x += Data.GetRect().Width;
  Data.SetString(text.at(pos));
  Data.SetPosition(x, y);
}


void Message::Drawing() {

  Display.DrawText(Data);
  Display.Refresh();
  Display.Blit(*Buffer);
 
}


Relevant Canvas structure :
Code: [Select]

void Canvas::Make(short ww, short hh) {
   w = ww; h = hh; x = y = 0;
   buff.Create(ww, hh); //buff = sf::RenderTexture
     buff.Clear(CLEAR);  
     img = this->buff.GetTexture(); //img = sf::Texture
     spr.SetTexture(img); //sf::Sprite
}

void Canvas::Set(float xx, float yy) {
  x = xx; y = yy;
  spr.SetPosition(x, y);
}

void Canvas::DrawText(sf::Text txt) {
  buff.Draw(txt);
  this->Refresh();
}

void Canvas::Blit(Canvas &bb) {
  img = buff.GetTexture();
  bb.Draw(*this);
}

void Canvas::Refresh() {
  buff.Display();
}
//pasted from header file for relevance:
    template<class Object>
     void Draw(Object &gfx) {
      buff.Draw(gfx.spr);
      this->Refresh();
}

6
Graphics / [Theory] Sprite Layering
« on: January 23, 2012, 03:46:54 pm »
Hello, I'm testing RPG sprite movement, and I want it to be so that the top part of a sprite always overlaps the bottom part of a sprite that's above it. The way I did it is below, and it works perfectly.

Basically, I create a map that holds all the sprites and arranges them according to their Y-coordinate. Since maps order their numerical keys from lowest to highest, iterating through it to blit each sprite guarantees that the ones further down are always drawn over ones further up.
If there's a more efficient way, I would love to know!

Code: [Select]

  map<float, Charaset*> Sprites;
  map<float, Charaset*>::iterator i;
    Sprites[(Spritesheet.Sheet.y)]  =  &Spritesheet;
//Spritesheet is the movable character; 2 and 3 are NPCs.
    Sprites[Sprites[Spritesheet2.Sheet.y]? (Spritesheet2.Sheet.y) -1: (Spritesheet2.Sheet.y)] = &Spritesheet2;
    Sprites[Sprites[Spritesheet3.Sheet.y]? (Spritesheet3.Sheet.y) -1: (Spritesheet3.Sheet.y)] = &Spritesheet3;
//The question operator is to ensure that sprites on the same Y-axis don't override each other.
 
  for (i = Sprites.begin(); i != Sprites.end(); ++i) {
    i->second->Sheet.Blit();
  }
  Sprites.clear();


7
Graphics / Some clarification please
« on: October 08, 2011, 10:41:37 pm »
The code you posted is fine--it's just that since the shape is an equiangular square, turning it 180 degrees is not going to change how it looks.

If you want it to progressively rotate to 180 degrees, that's a different story. The simplest way I can think of would be:

Code: [Select]

//Drawing loop
if (rect.GetRotation() < 180) {
  rect.Rotate(1.f);
}

8
Graphics / [Solved] SFML 2.0: strange rendering issue.
« on: October 08, 2011, 08:56:45 pm »
Aside from the topic, but you would be right--calling 'delete this' in the transition class when the game was shutting down causes undefined behavior because the Queue pointer it was assigned to was still getting called in the game loop. But that's been resolved, too. :)

9
Graphics / [Solved] SFML 2.0: strange rendering issue.
« on: October 08, 2011, 10:08:11 am »
Awesome! I figured out the problem myself! It seems like SFML didn't like when I set the Sprite directly to the texture of the RenderTexture. So basically anywhere in my code that read:


Code: [Select]
spr.SetTexture(buff.GetTexture());

I changed to:

Code: [Select]

Image img = buff.GetTexture();
spr.SetTexture(img);


VoilĂ ! Though I still do not understand why it rendered a sprite whose drawing function had not been called... that was actually borderline creepy.

10
Graphics / [Solved] SFML 2.0: strange rendering issue.
« on: October 07, 2011, 09:59:06 pm »
I installed SFML 2.0 Wednesday, and I've been tinkering around with it trying to make a basic shooter game.

After I press Start on the title screen, instead of the game screen, a scaled-to-screen and upside down version of the ship sprite is rendered. The weird this is, when I put the game in fullscreen and then back to windowed mode, it displays as it should. Even weirder? The huge sprite is rendered even I comment out the line to draw it in the game loop. o.O

I used to use Allegro and have a very specific way of coding that's probably not really intuitive to anyone but me. It makes me unsure of what code to post, but I'll try.

Graphic structure:
Code: [Select]

#include "Graphic.h"

//For future reference, I typdef-d sf::Image as Bitmap and sf::Texture as Image if anything looks confusing.

Graphic::Graphic(string file) {
  img.LoadFromFile(file);
    img.SetSmooth(false);
    spr.SetTexture(img);  

  x = spr.GetPosition().x;
  y = spr.GetPosition().y;
  w = spr.GetSize().x;  
  h = spr.GetSize().y;
  n = spr.GetRotation();
}

Graphic::Graphic(short w, short h) {
  img.Create(w, h);
  img.SetSmooth(false);
  spr.SetTexture(img);

  x = spr.GetPosition().x;
  y = spr.GetPosition().y;
  w = spr.GetSize().x;  
  h = spr.GetSize().y;
  n = spr.GetRotation();
}

void Graphic::Copy(Canvas &bb) {
  img = bb.buff.GetTexture();
  spr.SetTexture(img);
  Bitmap bmp = img.CopyToImage();
  bmp.SaveToFile("filer.png");
}

void Graphic::Blit(Canvas &buff = *Buffer) {
  buff.Draw(*this);
}

//Irrelevant code...


Canvas/Buffer structure:

Code: [Select]

Canvas *Buffer;

Canvas::Canvas(short ww, short hh) : w(ww), h(hh), x(0), y(0) {  
   buff.Create(w, h);
     buff.Clear(CLEAR);  //sf::Color(255, 0 , 255)
   spr.SetTexture(buff.GetTexture());
}

//Irrelevant code...

void Canvas::Tint(Color cc){
  spr.SetColor(cc);
}

//since it's a template, it's really in the h. file. Posted for clarity.
 template<class Object>
   void Draw(Object &gfx) {
    buff.Draw(gfx.spr); // buff is a sf::RenderTexture
    buff.Display();
  }

void Canvas::Refresh() {
  buff.Display();
  spr.SetTexture(buff.GetTexture());
}

void Canvas::Clear() {
  buff.Clear(CLEAR);  
}


Transition structure:
Code: [Select]

void Fader::Logic() {
  if (start.r > target.r) start.r -= speed;
  else if (start.r < target.r) start.r += speed;

  if (start.g > target.g) start.g -= speed;
  else if (start.g < target.g) start.g += speed;

  if (start.b > target.b) start.b -= speed;
  else if (start.b < target.b) start.b += speed;

  if (start.a > target.a) start.a -= speed;
  else if (start.a < target.a) start.a += speed;

  if (target.r == start.r && target.g == start.g &&
      target.b == start.b && target.a == start.a)
        GoToNext();
}

void Fader::Input() {
}

void Fader::Drawing() {
  Buffer->Clear();
  Copy->Blit();
  Buffer->Tint(start);
 
}

void Fader::GoToNext() {
 
  Buffer->Tint();
  if (next == END)
     Screen->Close();
   else if (next == TITLE)
      Queue = new Title();
   else if (next == ENGINE)
      Queue = new Engine();
  /* delete this;*/
}


Engine class:
Code: [Select]

#include "Engine.h"


Engine::Engine() : Started(no) {
    you.Make("Ship2.png"); //Calls the constructor of the Graphic structure
    you.spr.SetOrigin(14, 14);

}

Engine::~Engine(void) {
}

void Engine::Logic() {
 /*   Bitmap bmp = Screen->Capture();
    bmp.SaveToFile("wtf.png");
 */
}

void Engine::Input() {
 static sf::Clock Timer;

  if (Pressed.Tap(KEY_ENTER)) {
    delete this;
    Queue = new Fader(TITLE);
  }
   
 if (Pressed.Press(KEY_LEFT))
   you.MoveX(-3);
 if (Pressed.Hold(KEY_RIGHT))
  you.MoveX(+3);
  if (Pressed.Hold(KEY_DOWN))
   you.MoveY(+3);
 if (Pressed.Hold(KEY_UP))
   you.Rotate(-5);
 Timer.Reset();
}

void Engine::Drawing() {
  Buffer->Clear();
  Graphic *Sky = new Graphic("Night Sky.png");
    Sky->Blit();
    you.Blit(); //The error draws this even when  commented out! Wut?
  delete Sky;
}


Core class:
Code: [Select]

Irrelevant code...

void Core::Drawing() {  
  Screen->Clear(); // Screen is a sf::RenderWindow*
  Queue->Drawing();
  Buffer->Refresh();
  Screen->Draw(Buffer->spr);
  Screen->Display();
}


Assuming it's not a bug, it has to have something to do with the way I'm clearing and displaying the RenderTexture, likely doing something in the wrong order. Help!  :(

Pages: [1]