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

Author Topic: Drawing Images problem  (Read 7660 times)

0 Members and 1 Guest are viewing this topic.

Jimicro

  • Newbie
  • *
  • Posts: 31
    • View Profile
Drawing Images problem
« Reply #15 on: January 26, 2010, 07:08:22 pm »
Quote from: "Laurent"
Sorry, I don't understand what you want to compare.


Okey, I will try to explain again, I´m not so good at english :P

btw, I have 2 classes, one with character and one with tile images, and I do´t want the character to run through the wall, so I need a function to check so the character will stop if try run through the wall (image), I have done it before, but not now when they are in diffrent classes.

So I can´t just write if( character.getPosition().x <= wall.getPosition().x)

because they are in different classes. So is there any ideas?

I guess I have to do it like change  so the images is sprites with names. Like wall.getPosition for example. But I don´t now how.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Drawing Images problem
« Reply #16 on: January 26, 2010, 07:59:17 pm »
It will probably be easier if you show the real code (the two classes), and show in your code what you'd like to do and where ;)
Laurent Gomila - SFML developer

Jimicro

  • Newbie
  • *
  • Posts: 31
    • View Profile
Drawing Images problem
« Reply #17 on: January 26, 2010, 08:07:36 pm »
Quote from: "Laurent"
It will probably be easier if you show the real code (the two classes), and show in your code what you'd like to do and where ;)


Yeah I thought so too :)
Here is the view class again.

Code: [Select]

#include <SFML/Graphics.hpp>
#include <fstream>
#include <iostream>
#include "view.hpp"

using namespace sf;
using namespace std;

void Map::openFile()
{
    height = 0;

    //Read textfle (2D char array made up of a series of integers/letters)
   ifstream file;
   file.open("level.txt");

   std::string line;

    while(getline(file, line))
   {
      for(int width = 0; width < 12; width++)
      {
         level[height][width] = line.at(width);
      }
      height++;

   }
   //Close file stream
   file.close();
}
void Map::loadAllImages()
{
   if(!blockImage.LoadFromFile("cloud.jpg")||
   !groundImage.LoadFromFile("blocks.jpg") ||
   !wallImage.LoadFromFile("wall.jpg"))
   {
       //error
   }
}

void Map::draw(sf::RenderWindow& window)
{
    //Scroll through level and display corresponding image of tile
      for(int y = 0; y < 12; y++)
      {
         for(int x = 0; x < 12; x++)
         {
            num = level[y][x];

            if(num == '1')
            {

               this->sprite.SetPosition(x*50, y*50);
               this->sprite.SetImage(blockImage);

            }
            else if(num == '2')
            {
               this->sprite.SetPosition(x*50, y*50);
               this->sprite.SetImage(groundImage);
            }
            else if(num == '3')
            {
                this->sprite.SetPosition(x*50, y*50);
                this->sprite.SetImage(wallImage);
            }
            window.Draw(this->sprite);
         }
      }
}


You see here above, this->spriteSetImage(wallImage); for example. I want so I can make them to sprite. So I can compare the image size and position with the boy´s position and size.

main:

Code: [Select]
#include "animated.hpp"
#include "view.hpp"
#include "klocka.hpp"
#include <iostream>

using namespace sftools;

enum DIRECTION {UP = 0, RIGHT = 1, DOWN = 2, LEFT = 3};

/// Load an anim, with a constant format, from a picture.
Anim AnimFromImage(const sf::Image& img, int colone_px = 0)
{
    Anim ret;
    const int width_px(50);
    for (int i = 0; i < 4; ++i)
        ret.PushFrame(Frame(img,
                            sf::IntRect(width_px * i,
                                        colone_px * 64,
                                        width_px * (1 + i),
                                        (colone_px + 1) * 68)));
    return ret;
}

/// Update the boy's anim and made his moves.
void UpdateBoy(Animated& boy, const sf::Input& input, DIRECTION& direction,
float frame_time, Anim anims[4])
{

    // Update the boy.
    boy.Update();

    // If going upward...
    if (input.IsKeyDown(sf::Key::Up))
    {
        // Don't reset the anim.
        if (direction != UP)
        {
            boy.SetAnim(anims[UP], true); // Set the new anim, and play it.
            direction = UP;
        }
        else
            boy.Play(); // In case of not currently playing.

        // Move the boy.

    }

    // leftward...
    else if (input.IsKeyDown(sf::Key::Left))
    {
        // Working the same.
        if (direction != LEFT)
        {
            boy.SetAnim(anims[LEFT], true);
            direction = LEFT;
        }
        else
        boy.Play();

        boy.Move(-200 * frame_time, 0);
    }

    // rightward...
    else if (input.IsKeyDown(sf::Key::Right))
    {
        // Working the same.
        if (direction != RIGHT)
        {
            boy.SetAnim(anims[RIGHT], true);
            direction = RIGHT;
        }
        else
            boy.Play();

        boy.Move(200 * frame_time, 0);
    }

    // Doesn't move..
    else
    boy.Stop();
}

/// Our main function.
int main(int, char**)
{
    Map map;
    klocka Klocka;
    Klocka.i = 10;
    Klocka.b = 1;
    Clock Uhr;
    Klocka.ticka();

    map.loadAllImages();
    // Our beautiful window. :)
    sf::RenderWindow window(sf::VideoMode(600, 500, 32),
    "Spel");

    // Loading a picture with four anims.
    sf::Image char_anim;
    if (!char_anim.LoadFromFile("character2.png"))
    {
        std::cerr << "Error : cannot load character.png.\n";
        return EXIT_FAILURE;
    }
    char_anim.CreateMaskFromColor(sf::Color::Black);

    // Four anims : up, down, left, right.
    Anim go[4];
    for (int i = 0; i < 4; ++i)
        go[i] = AnimFromImage(char_anim, i);

    // In 'pause' and 'loop' mode ; frame time is 0.1 s ; looking upward :
    Animated boy(go[UP], 0.1f);
    boy.SetCenter(20, 32);
    boy.SetPosition(100, 350);
    DIRECTION direction = UP;

    // Main loop.
    sf::Event event;
    bool running = true;
    while (running)
    {
        // Events loop.
        while (window.GetEvent(event))
        {
            // Here we just deal with close event.
            // Moving is handle by sf::Input.
            switch (event.Type)
            {
                case sf::Event::Closed:
                    running = false;
                    break;

                case sf::Event::KeyReleased:
                    switch (event.Key.Code)
                    {
                        case sf::Key::Escape:
                            running = false;
                            break;

                        default:
                        break;
                    }
                    break;

            default:
            break;
            }
        }
        // Clear the screen with white color
        window.Clear(sf::Color(0, 0, 0));

        if(Uhr.GetElapsedTime() > 1)
        {
            Klocka.ticka();
            Uhr.Reset();
        }

        // Make all updates.
        UpdateBoy(boy, window.GetInput(), direction,
        window.GetFrameTime(), go);
        map.openFile();

        map.draw(window);
        // Draw our strings
        window.Draw(Klocka.Hello);
        // Draw everything.
        window.Draw(boy);
        window.Display();
    }

    return EXIT_SUCCESS;
}




Here is the main.

So you see I use the anim source from wiki.

Now you see main and view, I want so the boy will detect if there is a wall. But in the UpdateBoy I can write boy.getPosition().x <= 50) just to go throug the wall. But its just a very basic thing that not will work in the end. So I want to like if(boy.getPos().x <= tile.getPosition().x)

But the images is just images.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Drawing Images problem
« Reply #18 on: January 26, 2010, 08:29:02 pm »
What about a function such as "map.CheckCollision(boy)" ?
Laurent Gomila - SFML developer

Jimicro

  • Newbie
  • *
  • Posts: 31
    • View Profile
Drawing Images problem
« Reply #19 on: January 26, 2010, 08:30:36 pm »
Quote from: "Laurent"
What about a function such as "map.CheckCollision(boy)" ?


Yeah :) That should be nice. But where shall I write it? In the main or in view?

Jimicro

  • Newbie
  • *
  • Posts: 31
    • View Profile
Drawing Images problem
« Reply #20 on: January 26, 2010, 10:06:58 pm »
You can´t give me an example? I have tried all day long. But I can´t get it to work. I´m total stuck. Its just between the images and the boy where everything just not will work.

is there some way to get this to work? Everything would be so much easier I guess if it just was sprites all in just one main, but you can´t make a game without :P.

I guess I have to check collision in void UpdateBoy(), beacuse there is the movement, and there is the only function where I can change the speed.

Is there a way to implement one function of map, and all the sprites position to check with the boy? and every sprite has a individual abilty.

groundImage you should not fall down through, and the wall is you suppose not to go trough.

But I don´t know how. :(

Oz1571

  • Newbie
  • *
  • Posts: 28
    • View Profile
Drawing Images problem
« Reply #21 on: January 26, 2010, 10:18:58 pm »
I recommend reading up on the basics of pointers, that should help you do what you are trying to do. Unless I'm missing something. I don't know if this is the best forum for basic programming questions, unfortunately. I'm not trying to be rude or anything, I just think you might have more luck getting help elsewhere. :)

Jimicro

  • Newbie
  • *
  • Posts: 31
    • View Profile
Drawing Images problem
« Reply #22 on: January 26, 2010, 10:27:21 pm »
Quote from: "Oz1571"
I recommend reading up on the basics of pointers, that should help you do what you are trying to do. Unless I'm missing something. I don't know if this is the best forum for basic programming questions, unfortunately. I'm not trying to be rude or anything, I just think you might have more luck getting help elsewhere. :)


yeah I guess so :)

Jimicro

  • Newbie
  • *
  • Posts: 31
    • View Profile
Drawing Images problem
« Reply #23 on: January 28, 2010, 05:19:20 pm »
I got it to work now :) But which is the best way to check collision? GetPosition maybe isn´t the best way.

 

anything