Exiting glutFullScreen() - c++

I don't understand why when I press 'f' it enters into fullscreen but does not exit out of full screen. In the beginning of this method I have set bool fullscreen = false;
Here is the code for my toggle:
case 'f': //toggle screenmode
if(!fullscreen){
glutFullScreen();
fullscreen = true;
} else if(fullscreen){
glutReshapeWindow(1200, 900);
glutPositionWindow(0,0);
fullscreen = false;
}
break;

at the top of this method I have set bool fullscreen = false;
Every time you press a key, GLUT will call your keyboard handler. And at the top of your keyboard handler, you create a bool variable named fullscreen and set its value to false. This happens regardless of whether you're in full-screen mode or not. Every time you press a key, this will happen.
If you want to retain a boolean variable that actually tracks whether you're currently fullscreen, then you need to use a global. And you need to not set it at the start of the function. You set it once when you create the window, and you only set it again when you change the fullscreen status of the window.

To restore the original window size
... switch the calling order of ReshapeWindow and PositionWindow to
glutPositionWindow(0,0);
glutReshapeWindow(1200, 900);
Otherwise it will go back to windowed mode, but not adapt to the window size you specified!

The problem is not in the code you posted above as according to glutFullScreen specification the window should exit fullscreen mode once glutReshapeWindow or glutPositionWindow is being called.
at the top of this method I have set bool fullscreen = false;
I bet you set this inside the same function (not as a global variable) rendering the variable always being false when you press f

Instead of defining the bool at the beginning of the method, you have to define a global variable. Otherwise each time that method is called, it will set the fullscreen bool to 0, and think that it's not in fullscreen mode. Also, you may want to take not of euphrat's answer, he makes a valid point about the method organization.

Try this:
case 'f': //toggle screenmode
fullScreen = !fullScreen;
if (fullScreen) {
glutFullScreen();
} else {
glutReshapeWindow(1200, 900);
glutPositionWindow(0,0);
}
break;

While it might not answer the question directly, I found it an excellent place to post a go-to full-screen and exit source code.
Switch and restore full screen with <GL/glut.h>
myGame.c
...
glutSpecialFunc(handleSpecial);
void handleSpecial(int key, int x, int y) {
oglHandleFullScreen(key, x, y);
}
...
If you are looking forward to responding to a keyboard event instead (glutKeyboardFunc), make sure to change the signature of the below oglHandleFullScreen to (unsigned char key, int x, int y).
fullscreen.h
void oglHandleFullScreen(int key, int x, int y);
void oglWindowed(int positionX, int positionY, int width, int height);
void oglFullScreen();
fullscreen.c
#include <GL/glut.h>
#include "fullscreen.h"
int isFullScreen = 0;
int previousPosition[2] = { 0, 0 };
int previousSize[2] = { 100, 100 };
void oglHandleFullScreen(int key, int x, int y) {
if (key != GLUT_KEY_F11) { // Respond to F11 key (glutSpecialFunc).
return;
}
if (isFullScreen) {
oglWindowed(previousPosition[0], previousPosition[1],
previousSize[0], previousSize[1]);
} else {
previousPosition[0] = glutGet(GLUT_WINDOW_X);
previousPosition[1] = glutGet(GLUT_WINDOW_Y);
previousSize[0] = glutGet(GLUT_WINDOW_WIDTH);
previousSize[1] = glutGet(GLUT_WINDOW_HEIGHT);
oglFullScreen();
}
isFullScreen = !isFullScreen;
}
void oglWindowed(int positionX, int positionY, int width, int height) {
glutReshapeWindow(width, height);
glutPositionWindow(positionX, positionY);
}
void oglFullScreen() {
glutFullScreen();
}

In this case, you may want to add a static keyword before the bool to make this variable initialized only once.
A safer approach would be to make fullscreen global, since static local variables may be implemented differently across compilers and c++ standards.
Edit to add code snippet:
static bool fullscreen = false;
This should fix the problem of initializing the variable every call to the function, and by c++11 standard should be thread-safe
(Many compilers may say otherwise. Visual Studio only supported this feature of c++11 in VS2015 according to another question's accepted answer: Is static init thread-safe with VC2010?)
Better option may be using a global variable (Wrapping into a class is not good for this purpose).
Edit: Also, despite being simple in syntax, this c++11 feature is costly when called frequently.(See Cost of thread-safe local static variable initialization in C++11?)
(A key down is not that frequent as drawing vertices at high FPS, though... Do NOT use this when performance-critical...)

Related

Movement of Object in a game in C++

So I am making a game for a school project. You might be familiar with this one. Its Arkanoid, or the game in which a ball is used to destroy bricks and is deflected on by a platform.
Currently I am stuck at a point. I have got an idea of how to move my platform using _getch(), but when I put in a ball it is also moving on a key press. I cant figure out on how to run it simultaneously with or without any key presses. Or if there is way to skip a key press every time until it is registered.
Here is my code so far. The movement of the ball is not complete it is just a prototype right now. Any help would be appreciated.
I am using a graphics library provided by my school but you can use the C++ graphics library.
#include <iostream>
#include "myconsole.h"
#include "mygraphics.h"
#include <conio.h>
#include <string>
using namespace std;
void rect() {
COLORREF White = RGB(255, 255, 255);
COLORREF Blue = RGB(135, 206, 235);
// x1 y x2 y
myRect(400, 475, 550, 480, Blue, Blue);
_getch();
}
void circle() {
COLORREF Red = RGB(255, 0, 0);
myEllipse(0, 50, 300, 350, Red, Red);
_getch();
}
void moverect() {
COLORREF White = RGB(255, 255, 255);
COLORREF Blue = RGB(135, 206, 235);
char _x;
const char _r = 'd';
const char _l = 'a';
int x1 = 400;
int x2 = 550;
int by1 = 455;
int by2 = 475;
int m = 48;
while (1) {
_x = _getch();
system("cls");
if (_x == _r) {
if (x2 < 970) {
x1 += 10;
x2 += 10;
for (int i = 0; i < 10; i++) {
myRect(x1++, 475, x2++, 480, Blue, Blue);
}
}
else
myRect(x1, 475, x2, 480, Blue, Blue);
}
else if (_x == _l) {
if (x1 > 0) {
x1 -= 10;
x2 -= 10;
for (int i = 0; i < 10; i++) {
myRect(x1--, 475, x2--, 480, Blue, Blue);
}
}
else
myRect(x1, 475, x2, 480, Blue, Blue);
}
myEllipse(463, by1 -= 10, 487, by2 -= 10, White, White);
}
}
int main() {
{
moverect();
return 0;
}
}
Since you seem to be using Windows and Microsoft Visual C++ you could use _kbhit(), which will tell you if a key is pressed. Note that this is not standard C++.
This might look like:
if (_kbhit()) {
_x = _getch();
}
else {
_x = -1;
}
Alternatively, others mentioned threads, although John also noted that _getch() is also not standard C++, but I thought introducing some of the concepts of threads might be helpful.There are multiple ways to do it with threads but I will show what I consider to be the easiest way.
First we will create a class, this is to hide a variable that contains the latest character. We want it hidden so that when it is read from the main thread, the latest character is consumed (set to -1) so that when we next call the function, if another character is yet to be input the function will return -1.
Note that if two threads read and write from a variable at the same time, undefined behaviour occurs, for this reason we are going to use an atomic variable as it is defined when two threads try to access it at the same time. Alternatively mutexes exist, they are slower but allow for more complex types to be locked and unlocked so that only one thread may access them at a time.
class LatestChar {
public:
/// initialize latest character to -1
LatestChar() :
latest_char{ -1 }
{}
/// returns latest character or -1 if there is no latest character
int getLatestChar() {
// get latest character
int temp_latest_char{ latest_char };
// consume latest character (so if this is called again, -1 is returned)
latest_char = -1;
// return latest character
return temp_latest_char;
}
private:
/// equal to latest character or -1 when there is no character to get
///
/// this is atomic because it might be read from the main thread at the same
/// time it is written from the getch thread.
std::atomic_int latest_char;
};
Next we need to create the thread that calls _getch(). This is a member variable and will be declared in the constuctor, where it will start running and getting the latest character and repeating.
class LatestChar {
public:
/// initialize latest character to -1,
/// and starts thread that gets the latest character
LatestChar() :
...,
getch_thread([this] {
while (true) {
latest_char = _getch();
}
})
{}
...
private:
...
/// this thread loops until told to stop, and updates the latest character
std::thread getch_thread;
};
Almost done, we now need to rejoin the thread at the end of the program, we will use another atomic variable, this time a boolean, to represent a flag to say "Hey, we are done getting characters, finish what you are doing and join back up with the main thread.
Note that the getch thread will only check this after it reads a character, so when you close your program you might need to input one more character, the alternative is to call something like std::terminate() which will stop the entire program forcefully, which is probably not desired when it can be avoided.
We will initialize the flag to false in the constructor (make sure it is initialized before the getch thread is, otherwise the getch thread might check the value before it has been set to false. The order is they are initialized is the same order they are declared in the class definition.), check it in the thread, and set it to true in the destructor and then also call the join function which will wait until the thread is finished and connect join it back into main.
class LatestChar {
public:
/// initialize latest character to -1, end thread flag to false,
/// and starts thread that gets the latest character
LatestChar() :
...,
end_thread{ false },
getch_thread([this] {
while (!end_thread) {
latest_char = _getch();
}
})
{}
/// sets end thread flag to true and joins getch thread with main thread
~LatestChar() {
end_thread = true;
getch_thread.join();
}
...
private:
...
/// this flag tells the getch thread to stop, like at the end of a program,
/// _getch() will need to receive a character before this is checked
///
/// this is atomic because it might be read from the getch thread at the
/// same time it is written from the main thread.
///
/// make sure this is initialized before the getch thread begins
std::atomic_bool end_thread;
...
};
Finally, let's instantiate the class and call the function.
// somewhere not in a loop
LatestChar latest_char;
...
// somewhere is a loop
_x = latest_char.getLatestChar();

GetAsyncKeyState constantly active

I am trying to make an autoclicker to mess around with getkeystate and key pressing functions in VS and c++, for some odd reason it will never stop clicking once it is initially clicked. I looked over my code and couldn't find anything wrong with it, i know the issue is gonna be something stupidly small. Heres my code:
#include <iostream>
#include <Windows.h>
using namespace std;
bool click = false;
int x = 0, y = 0, cps;
void gui()
{
cout << "Enter desired clicks per second: ";
cin >> cps;
}
void clicked()
{
while (1)
{
if (GetAsyncKeyState(VK_LBUTTON)) // Left mbutton
{
click = true;
}
else
{
click = false;
}
if (click == true)
{
mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
Sleep(1000 / cps);
}
if (click == false)
{
continue;
}
}
}
int main()
{
gui();
clicked();
}```
You maybe missed to read the documentation about return value of GetAsyncKeyState.
The return value of GetAsyncKeyState is zero for the following cases:
The current desktop is not the active desktop
The foreground thread belongs to another process and the desktop does not allow the hook or the journal record.
And,if the most significant bit is set , the key is down.
So, for checking up, you need to check, if the most significant bit is reset.
[Sorry, I am writing the answer on mobile. So, could not provide source code.]

C++: Instantiate same object as one of many types depending on variable

I am programming an LED Cube I have designed. The cube has a "pause" button and a "play/next" button. Unless the cube is paused, it will cycle through all of the different effects (animations) I've made for it. If you press the pause button, the cube will no longer transition between effects and will instead repeat the current effect. Pressing the 'play/next' button will unset the pause feature and will advance to the next effect immediately.
Some of these effects are pretty complex and require a large number of variables to be kept between frames of animation. In order to easily destroy all of these variables at a moment's notice (like when the next button is pressed), I'm instantiating the current animation as an object and destroying it when the effect is complete or the skip button is pressed.
I'm trying to set my main loop up as follows:
void loop() {
//create an effect object
switch(effectIndex){
case 0:
EF_GROWFRAME effect;
break;
case 1:
EF_RANDOMFILL effect;
break;
}
bool proceed;
do{
//returns false until the effect has completed
proceed=effect.step();
//push this cube update and wait for it to display
cube.update();
cube.waitForFrame();
}
while ((!proceed)&&(!skipflag));
//skipflag is set true during a timer interrupt if the skip button is freshly pressed
skipflag=false;
cube.clearPattern();
if (play) effectIndex++;
if (effectIndex=effectCount) effectIndex=0;
}
That fails because of my conflicting definitions of effect though. You can probably see what I'm going for, so what's the proper way to approach this?
This is a use case for polymorphism.
Define a base class, Animation that defines a shared interface and have your various animation types derive from it. For example:
class Animation {
public:
virtual ~Animation() {
// any generic cleanup shared by all animation types
}
virtual bool step() = 0;
};
class AnimationA : public Animation {
public:
bool step() override {
// logic for this type of animation
}
};
class AnimationB : public Animation {
public:
bool step() override {
// logic for this type of animation
}
};
void loop() {
std::unique_ptr<Animation> effect;
switch (effectIndex) {
case 0:
effect = std::make_unique<AnimationA>();
break;
case 1:
effect = std::make_unique<AnimationB>();
break;
}
//...
}
Live Demo
Since it seems like this may be an embedded environment, you could avoid the dynamic memory allocation from my first example by factoring your animation playing logic out into a separate function:
void playAnimation(Animation& effect) {
bool proceed;
do{
//returns false until the effect has completed
proceed=effect.step();
//push this cube update and wait for it to display
cube.update();
cube.waitForFrame();
} while (!proceed && !skipFlag);
//skipflag is set true during a timer interrupt if the skip button is freshly pressed
skipflag=false;
cube.clearPattern();
}
void loop() {
switch (effectIndex) {
case 0:
{
AnimationA effect;
playAnimation(effect);
break;
}
case 1:
{
AnimationB effect;
playAnimation(effect);
break;
}
}
if (play) effectIndex++;
if (effectIndex == effectCount) effectIndex=0;
}
Live Demo

C++. gtk_window_resize doesn't resize window

I've got class (called BorderWindow) which is in fact wrap around GtkWidget. This class represents border around specific application's window (lets say Terminal window, for instance). Inside of BorderWindow constructor I create timer so every second BorderWindow::ClockTick function is called.
Inside of this function border size (which is actually GtkWidget) is compared with bound application's window (Terminal in our case) and if it differs gtk_window_resize is called. However, if I call then gtk_window_get_size it returns old values.
gboolean BorderWindow::ClockTick(gpointer data)
{
auto that = reinterpret_cast<BorderWindow*>(data);
int x = 0, y = 0;
GtkWindow* pWindow = GTK_WINDOW(that->m_borderWindowHandle);
gtk_window_get_size(pWindow, &x, &y);
DUMPER_INFO("curr size: %dx%d; new size: %dx%d", x, y, that->m_windowRect.width, that->m_windowRect.height); // added for debug
if(x != that->m_windowRect.width || y != that->m_windowRect.height)
{
gtk_window_resize(pWindow, that->m_windowRect.width, that->m_windowRect.height);
that->CreateBorder();
GtkWindow* pWindow = GTK_WINDOW(that->m_borderWindowHandle); // added for debug
gtk_window_get_size(pWindow, &x, &y); // added for debug
DUMPER_INFO("after resize: %dx%d", x, y); // added for debug
}
gtk_window_get_position(pWindow, &x, &y);
if(x != that->m_windowRect.x || y != that->m_windowRect.y)
{
gtk_window_move(pWindow, that->m_windowRect.x, that->m_windowRect.y);
}
gdk_window_invalidate_rect(gtk_widget_get_window(that->m_borderWindowHandle), nullptr, FALSE);
that->m_highlightFrame = !that->m_highlightFrame;
return TRUE;
}
Here is debug output
20-05-19 11:24:40.294 [139856177248000] INFO 1537 %% - static gboolean LinuxBorderWindow::ClockTick(gpointer): curr size: 734x540; new size: 1024x706
20-05-19 11:24:40.295 [139856177248000] INFO 1537 %% - static gboolean LinuxBorderWindow::ClockTick(gpointer): after resize: 734x540
So as you can see, the window wasn't resized.
This code snippet works fine for xfce and Unity DEs, but doesn't work for GNOME (and GNOME classic).
Could anyone explain what I'm doing wrong and how to resize window for GNOME DE?
Thanks.

GLFW input handling not working as intended

So this may be a newbie question regarding GLFW, but I seem to be having an interesting issue. So I'm developing a simple input handling class using GLFW, specifically utilizing static methods to allow only requiring the inclusion of the header file to use the methods. So this is my code thus far...
InputHandler.cpp
#include "InputHandler.h"
GLFWwindow *Input::m_Window;
bool Input::isDown;
std::vector<int> Input::keyCache;
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
for (int _key = 0; _key < Input::keyCache.size(); _key++)
{
if (key == Input::keyCache[_key] && action == GLFW_PRESS || key == Input::keyCache[_key] && action == GLFW_REPEAT)
Input::isDown = true;
else
Input::isDown = false;
}
}
void Input::processInput(GLFWwindow* window)
{
m_Window = window;
}
bool Input::isKeyDown(int key)
{
keyCache.push_back(key);
glfwSetKeyCallback(m_Window, key_callback);
return isDown;
}
InputHandler.h
#pragma once
#include <GLFW\glfw3.h>
#include <vector>
class Input
{
public:
static bool isDown;
static std::vector<int> keyCache;
private:
static GLFWwindow *m_Window;
public:
static void processInput(GLFWwindow* window);
static bool isKeyDown(int key);
static bool isKeyUp(int key);
static int getMouseX();
static int getMouseY();
};
However, whenever I call the isKeyDown method, which will return true or false based on if the key is down or not, multiple times, the program seems to only respond to the last key mentioned. For example, if I use the code...
if (Input::isKeyDown(GLFW_KEY_W) || Input::isKeyDown(GLFW_KEY_Q))
std::cout << "Key is down" << std::endl;
only the Q key will trigger the statement, W does nothing. I have gone through GLFW's site multiple times, the input guide is where I learned the necessary things needed to receive the input, and it also seems that no one else has had this issue, as I have searched and searched for anything. If anyone could help, by explaining possible issues or pointing me in the right direction in terms of finding the answer myself, I would greatly appreciate it!
Alright, I figured out my issue. I was WAAAAAAAAAAAAAAAAAAAAAAAAAAY overcomplicating it. So, my issue was that the key callback was only able to ever handle one key, and it was completely unnecessary. So, my fix involved removing the key callback, the keyCache and the isDown variable, and simply changing isKeyDown to...
bool Input::isKeyDown(int key)
{
if (glfwGetKey(m_Window, key))
return true;
return false;
}
I'm sorry for how ridiculously incompetent I have shown myself to be. Well, now I know how the key callback works! xD