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

Author Topic: Texture how to load area image  (Read 8903 times)

0 Members and 1 Guest are viewing this topic.

BIOS

  • Newbie
  • *
  • Posts: 7
    • View Profile
Texture how to load area image
« on: August 20, 2017, 02:57:02 pm »
Hello the community, I have a little problem. I don't know the structure to load a specific area in a texture.

Look at my code:

#include <SFML/Audio.h>
#include <SFML/Graphics.h>
#include <SFML/Config.h>
#include <stdlib.h>
#include "my.h"

int     map_creation(sfRenderWindow* window)
{
  sfTexture* mtexture;
  sfTexture* ptexture;
  sfSprite* map;
  sfSprite* player;
  sfEvent event;

  mtexture = sfTexture_createFromFile("map1.png", NULL);
  if(!mtexture)
    return EXIT_FAILURE;
  map = sfSprite_create();
  sfSprite_setTexture(map, mtexture, sfTrue);

//In here im not sure about the stIntRect(10, 10, 32, 32)
  ptexture = sfTexture_createFromFile("sprite.png", sfIntRect(10, 10, 32, 32));
  if (!ptexture)
    return EXIT_FAILURE;
  player = sfSprite_create();
  sfSprite_setTexture(player, ptexture, sfTrue);

  while (sfRenderWindow_isOpen(window))
    {
      sfRenderWindow_clear(window, sfBlack);
      sfRenderWindow_drawSprite(window, map, NULL);
      sfRenderWindow_display(window);
     
      while (sfRenderWindow_pollEvent(window, &event))
        {
          if (event.type == sfEvtClosed)
            sfRenderWindow_close(window);
        }
    }
 
  return (0);
}
 

The console says :
gcc -o tekadventure main.c tekadventure.c -W -Wall -Werror -lcsfml-graphics -lcsfml-audio -lm libmy.a
tekadventure.c: In function ‘map_creation’:
tekadventure.c:30:53: error: expected expression before ‘sfIntRect’
   ptexture = sfTexture_createFromFile("sprite.png", sfIntRect(10, 10, 32, 32));
                                                     ^
Makefile:26: recipe for target 'tekadventure' failed
make: *** [tekadventure] Error 1


I know I did not write it the right way, I tried to look at SFML c++ and adapt it to the CSFML API but is not working.

Thank you in advance.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Texture how to load area image
« Reply #1 on: August 20, 2017, 07:34:20 pm »
You're in C now, so sfIntRect doesn't create an object and it's not a function. Instead you first need to create and initialize a sfIntRect struct.

So what's your reason for using the C binding?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

BIOS

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Texture how to load area image
« Reply #2 on: August 21, 2017, 10:48:11 am »
Thank you for your answer. I am using C binding because I am new to programming and the first language that I am learning is C.

I did initialize the structure like below but I'm not sure how to use it in the function sfTexture_createFromFile().
And the documentation doesn't say much about it.

My example:
#include <SFML/Audio.h>
#include <SFML/Graphics.h>
#include <SFML/Config.h>
#include <stdlib.h>
#include "my.h"

  struct sfIntRect {
    int left;
    int top;
    int width;
    int height;
  };

int     map_creation(sfRenderWindow* window)
{
  sfTexture* ptexture;
  sfSprite* player;
  sfEvent event;
 
#This line
  ptexture = sfTexture_createFromFile("sprite.png",  10, 10, 32, 32);
  if (!ptexture)
    return EXIT_FAILURE;
  player = sfSprite_create();
  sfSprite_setTexture(player, ptexture, sfTrue);

 


 

 

anything