can't draw an SFML sprite when sprite has a negative position - drawing

Whenever I try to draw an SFML sprite with a negative position, the sprite will not be drawn to the screen. I have tried with and without a view applied to the window, but the sprite is not rendered either time.
I have a sprite with a texture on it (size 32*64). I try drawing in at position (-1,-1) but it will not work. Is this intentional behaviour. If so how would I go about drawing sprites with negative positions.
thanks

sf::Texture texture;
if (!texture.loadFromFile("texture.png")) {
std::cout << "Error rendering Object";
return 0;
}
sf::Sprite Sprite;
Sprite.setTexture(texture);
Sprite.setPosition(sf::Vector2f(-5, -5));
And draw sprite > window.draw(Sprite);

Turns out that I had some confusing mix-ups with signed and unsigned ints. This caused the sprites to be unable to be drawn in negative positions. Please check signed-unsigned types if this error occured for you.

Related

C++ sfml how to rotate a character from its center

I want to use the function sprite.rotate() and sprite.setrotation() but sfml takes into account the top-right point of the sprite. How to set this point to sprite center?
If you want to set the rotation center you have to use sprite.setOrigin() function which sets the center for position and rotation.
For example:
sf::Texture texture;
if (!texture.loadFromFile("texture.png"))
exit(-1);
sf::Sprite sprite;
sprite.setOrigin((sf::Vector2f)texture.getSize() / 2.f);
Or if you are changing the texture rect of the sprite:
sprite.setOrigin(sf::Vector2f(sprite.getLocalBounds().width, sprite.getLocalBounds().height) / 2.f);

SDL draw image on screen using screen coordinates with multiple viewports

I currently have two viewports in my game. One is for the information in the top (health, gold, magic, etc) and the other viewport shows the map area. I created my own custom mouse cursor using an image and I'm displaying that image just like any other and update the position based on my mouse cursor position. My only problem is that I can only draw that image on a viewport. I have it set the show up on the map area, and when I move my mouse to the top; it won't display on the information area. It just leaves the window. So the first thing I did (I knew it wouldn't work, but tested it anyways) was to draw the mouse on both viewports. But that shows two mouse cursors when moving to the top of the screen (LOL). My question is how can I get the mouse image to draw on the screen using screen (or window) coordinates, outside of the viewport. Does that make sense? I want to be able to move the mouse anywhere on the screen so I can click on items in the map viewport as well as the information viewport.
My viewport class is very simple:
Viewport.h
#pragma once
#include <SDL.h>
class Viewport
{
public:
Viewport(int x, int y, int width, int height);
~Viewport();
SDL_Rect GetViewport();
private:
SDL_Rect viewport;
};
Viewport.cpp
#include "Viewport.h"
Viewport::Viewport(int x, int y, int width, int height)
{
viewport.x = x;
viewport.y = y;
viewport.w = width;
viewport.h = height;
}
Viewport::~Viewport()
{
}
SDL_Rect Viewport::GetViewport()
{
return viewport;
}
In my Game class I initialize two variables for each Viewport
Viewport hud;
Viewport arena;
In the constructor I initialize them to the appropriate sizes. Then In my game drawing function I set the viewports accordingly and draw in the appropriate viewport.
// gfx is my Graphics class. Everything that has to do with drawing is inside that class; including setting the viewport for the render.
gfx.SetViewport(hud.GetViewport());
// This is where I would draw stuff on the hud viewport
gfx.SetViewport(arena.GetViewport());
// Where I draw the map, enemies, etc.
my SetViewport function is setup like this:
void Graphics::SetViewport(SDL_Rect viewport)
{
SDL_RenderSetViewport(renderer, &viewport);
}
How can I get my mouse image to be drawn (with the viewport not relavant). Below are some images of what I'm talking about.
Only displaying on the arena and won't show on the hud.
Displays the mouse on both if I draw the mouse on both viewports
Edit: I made a workaround which will work for this game
I'm just worried that it won't be good for performance on a bigger and larger game
I created another viewport that is as large as the screen (window) and I'm drawing the mouse to that viewport.
Super late answer but here it is. Calling SDL_RenderSetViewport(renderer, NULL) will reset the viewport to the entire window.
You should do this before you call SDL_PollEvent as well because if you poll mouse events when a viewport is active the mouse motion event coordinates will be relative to the viewport and not the main window.

draw only selected item from a scene in opengl

I am using OpenGL Shading language along with SDL. I have a static scene with moving objects and the general structure of program is as follows:
initialize(){
//init meshes
}
display(){
//draw commands for static scene
//draw commands for moving objects
}
main()
{
initialize()
while(repeat)
{
display();
//swap buffers or flush drawing commands
}
}
I need to draw static 'scene' but drawing it only once can be sufficient unlike moving objects. Is there a way to redraw only what needs to be redrawn?
If you're making 3D game, you can put entire static scene into single VBO and then draw it with single function call.
If you're making 2D game, you can draw it to texture and then draw a fullscreen quad with this texture.

can't draw my rectangle where I want on the screen

I need to draw case/ spare which are Rectangle.
but the problem is that glTranslated()doesn't work. When I call it my rectangle isn't draw anymore.
my rectangle: glRectf(-0.032f, 0.032f, 0.032f, -0.032f);
(I don't understand values but working)
full code:
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3ub(254,128,1);
//glTranslated(50, 20, 0); when I remove the comment, my rectangle doesn't appear.
glRectf(-0.032f, 0.032f, 0.032f, -0.032f);
glFlush();
SDL_GL_SwapBuffers();
}
glRectf(x1,y1,x2,y2) means draw a rectangle from point (-0.032,0.032) to the diagonally opposite corner at (0.032,-0.032).
glTranslated(50,20,0) is applying a vector to move it. Those are pretty big numbers since you are saying you can see the rectangle (square) of size 0.064 wide. It may be drawing out of your viewport so you can't see it.
Try some small numbers and also glTranslated requires doubles (so I would cast them too).
Read more here...
http://www.cprogramming.com/tutorial/opengl_first_opengl_program.html

LWJGL 3D picking

So I have been trying to understand the concept of 3D picking but as I can't find any video guides nor any concrete guides that actually speak English, it is proving to be very difficult. If anyone is well experienced with 3D picking in LWJGL, could you give me an example with line by line explanation of what everything means. I should mention that all I am trying to do it shoot the ray out of the center of the screen (not where the mouse is) and have it detect just a normal cube (rendered in 6 QUADS).
Though I am not an expert with 3D picking, I have done it before, so I will try to explain.
You mentioned that you want to shoot a ray, rather than go by mouse position; as long as this ray is parallel to the screen, this method will still work, just the same as it will for a random screen coordinate. If not, and you actually wish to shoot a ray out, angled in some direction, things get a little more complicated, but I will not go in to it (yet).
Now how about some code?
Object* picking3D(int screenX, int screenY){
//Disable any lighting or textures
glDisable(GL_LIGHTING);
glDisable(GL_TEXTURE);
//Render Scene
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
orientateCamera();
for(int i = 0; i < objectListSize; i++){
GLubyte blue = i%256;
GLubyte green = min((int)((float)i/256), 255);
GLubyte red = min((int)((float)i/256/256), 255);
glColor3ub(red, green, blue);
orientateObject(i);
renderObject(i);
}
//Get the pixel
GLubyte pixelColors[3];
glReadPixels(screenX, screenY, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixelColors);
//Calculate index
int index = pixelsColors[0]*256*256 + pixelsColors[1]*256 + pixelColors[2];
//Return the object
return getObject(index);
}
Code Notes:
screenX is the x location of the pixel, and screenY is the y location of the pixel (in screen coordinates)
orientateCamera() simply calls any glTranslate, glRotate, glMultMatrix, etc. needed to position (and rotate) the camera in your scene
orientateObject(i) does the same as orientateCamera, except for object 'i' in your scene
when I 'calculate the index', I am really just undoing the math I performed during the rendering to get the index back
The idea behind this method is that each object will be rendered exactly how the user sees it, except that all of a model is a solid colour. Then, you check the colour of the pixel for the screen coordinate requested, and which ever model the colour is indexed to: that's your object!
I do recommend, however, adding a check for the background color (or your glClearColor), just in case you don't actually hit any objects.
Please ask for further explanation if necessary.