SFML C# How do I switch between fullscreen? - sfml.net

I would like to switch between fullscreen and windowed mode. Is there some way to do that without restarting whole game?

I would suggest calling the create method on your window (C++, but I'm sure it works in C# as well):
yourWindow->create(sf::VideoMode::getDesktopMode(), "Title", sf::Style::Fullscreen);

Under C# you can't access ->create like C. you have to close the old RenderWindows and create a new one.
if (Keyboard.IsKeyPressed(Keyboard.Key.Enter) && Keyboard.IsKeyPressed(Keyboard.Key.LAlt) && !auxChangeStyle)
{
auxStyle = (auxStyle == Styles.Default)? Styles.Fullscreen: Styles.Default;
window.Close();
window = new RenderWindow(new VideoMode(800, 600), "Title", auxStyle);
window.Closed += (_, __) => window.Close(); //reassign the event handlers
}

Related

Context menu does not consistently work on arch linux?

I am using arch linux and a basic cpp xlib custom window manager. However, every time I right click to open the context menu it just flickers and disappears. I cannot use it at all. I also cannot use top drop down menus (file, edit, about, ect.) on any application. Is there anything in Xlib which I have to look out for to ensure I may use the context menus normally?
This is the case in every application I have tried. Only clue I have is in brave it occasionally displays the following message:
XGetWindowAttributes failed for window [WINDOW_ID]
The following simplified example also has this issue:
int main()
{
display = XOpenDisplay(nullptr);
root = DefaultRootWindow(display);
XSelectInput(display, root, SubstructureRedirectMask | SubstructureNotifyMask | StructureNotifyMask);
XGrabServer(display);
Window returned_root;
Window returned_parent;
Window* top_level_windows;
unsigned int num_top_level_windows;
XQueryTree(display, root, &returned_root, &returned_parent, &top_level_windows, &num_top_level_windows);
for(unsigned int i = 0; i < num_top_level_windows; ++i)
{
Frame(top_level_windows[i], true);
}
XFree(top_level_windows);
XUngrabServer(display);
for(;;)
{
XEvent event;
XNextEvent(display, &event);
switch (event.type)
{
case MapRequest:
{
Frame(event.xmaprequest.window, false);
XMapWindow(display, event.xmaprequest.window);
break;
}
case ButtonPress:
XRaiseWindow(display, event.xbutton.window);
break;
}
}
return true;
}
void Frame(Window window, bool created_before_manager)
{
//Retrieve attributes of window to frame
XWindowAttributes attr = {0};
XGetWindowAttributes(display, window, &attr);
//If window was created before window manager started, we should frame it only if it is visible and does not set override_redirect
if(created_before_manager && (attr.override_redirect || attr.map_state != IsViewable))
{
return;
}
//Create frame
Window frame = XCreateSimpleWindow(display, root, attr.x, attr.y, attr.width, attr.height, 5, 0xff0000, 0xffffff);
XReparentWindow(display, window, frame, 0, 0);
XMapWindow(display, frame);
XGrabButton(display, Button1Mask, Mod1Mask, window, None, ButtonPressMask, GrabModeAsync, GrabModeAsync, None, None);
}
To be clear it also works with a super simple example such as:
int main()
{
Display* display = XOpenDisplay(nullptr);
for(;;) {}
return true;
}
The reason I believe the window manager is at fault is because this issue only occurs after I run the window manager.
I expected this to work out of the box. I have not found any information on context menus needing special treatment. They do have the override_redirect flag set to true, so I do not frame them. I cannot find information on any other special treatment required.
It is necessary to make sure the client window has input. I had the input set to whatever was clicked (frame, title bar, or client) because it worked fine as far as normal input is concerned. However, the context menus will only work if you make sure the input is set to the client window directly.

How to watch any window movements with xlib?

How do I track move event for all the windows?
Like if user moves window "Pluma" my daemon would receive window name and new coordinates.
if(XCheckMaskEvent(display, -1, &event))
{
if(event.type == ConfigureNotify)
{
moved += event.xmotion.x + event.xmotion.y;
//qDebug << moved;
}
}
I tried tracking it like this, but it does not work...
You need t select SubstructureNotify mask on the root window first:
XSelectInput(display, XDefaultRootWindow(display), SubstructureNotifyMask );
This way you are telling X server "I'm interested in root window childrens move/resize/delete/create events"

How can you efficiently create an allegro 5 title menu?

I'm working on my first game in Allegro 5, I've got the title menu rendering as such, however I want to add clickable text in the menu. How would I make it so that, when you hover over the text you can click it? I'm thinking having a for statement checking the pixels would be very bad for performance, here's what I have so far:
#include <allegro5\allegro.h>
#include <allegro5\allegro_image.h>
#include <allegro5\allegro_primitives.h>
const int width = 1280;
const int height = 720;
int main(void)
{
al_init();
al_init_primitives_addon();
al_init_image_addon();
ALLEGRO_DISPLAY *display = al_create_display(width, height);
ALLEGRO_BITMAP *title = al_load_bitmap("titlemenu.bmp");
al_clear_to_color(al_map_rgb(0, 0, 0));
al_draw_bitmap(title, 0, 0, 0);
al_flip_display();
al_rest(3.0);
al_destroy_display(display);
return 0;
}
I'm using codeblocks on windows XP SP3
To do it "properly," you'd need to use some sort of GUI library. But you can easily create a clickable section of the screen by hardcoding some rectangle's coordinates.
First you'll need to set up your event handling:
ALLEGRO_EVENT_QUEUE *queue;
queue = al_create_event_queue();
al_install_keyboard();
al_register_event_source(queue, al_get_keyboard_event_source());
Without getting into the specifics of event handling (it's an entire topic of its own), here's the relevant bit:
int selection = 0;
while (!selection)
{
ALLEGRO_EVENT event;
al_wait_for_event(queue, &event);
if (event.type == ALLEGRO_EVENT_KEY_UP)
{
if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
selection = MYGAME_QUIT;
}
else if (event.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP)
{
if (event.mouse.x >= MYGAME_MENU_X1 && event.mouse.x < MYGAME_MENU_X2 &&
event.mouse.y >= MYGAME_MENU_Y1 && event.mouse.y < MYGAME_MENU_Y2)
{
selection = MYGAME_OPTION1;
}
}
}
There are many ways to improve upon this example... This is just to get you started.
You should carefully read through the documentation regarding event handling and examine the bundled examples and check out the wiki for more information.
PS: Use forward slashes when using file paths, as they are cross platform:
#include <allegro5/allegro.h>
The above isn't true;
You can add the keyboard to your game and then you have 2 options for collision detection, pixel perfect and bounding box, both covered in the allegro 5 wiki. The tick here to create the best menu IMO is to create a loop and timer for the menu, then list the keyboard, mouse, and timer events seperatley. Next make some if statements so the mouse or keyboard event only triggers when you actually click it, this is needed so you can scroll through a menu with both mouse and keypad, or you could just make it so the mouse wont affect the screen until after you click, but former looks better, at least IMO.

Qt::Tool appear too fast when parent restored

I have a qt application, window with several child widgets that are Qt::Tool windows.
When I minimize window and then restore on windows 7 child widgets appear immediately, but parent window not because of animation.
Is there any way to prevent this behavior and make child windows appear after main window?
Why don't you hide the tool windows while the main window is restoring? You can use this piece of code and adapt it to your needs:
void MainWindow::changeEvent( QEvent* e )
{
if ( e->type() == QEvent::WindowStateChange )
{
QWindowStateChangeEvent* event =
static_cast< QWindowStateChangeEvent* >( e );
if ( event->oldState() & Qt::WindowMinimized ) {
qDebug() << "The Window has been restored!";
}
else if ( (event->oldState() == Qt::WindowNoState) &&
(this->windowState() == Qt::WindowMaximized) ) {
qDebug() << "the window has been Maximized!";
}
}
}
For example, you can start a timer for a certain amount of time, and have the slot connected to your child tool windows, making them appear as you wish.
You can also play with transparency in the child tool windows, and emulate the main window effects.
As I recall, something like this will do:
setStyleSheet("background:transparent;");
setAttribute(Qt::WA_TranslucentBackground);
setWindowFlags(Qt::FramelessWindowHint);
Hope that helped!

How can I tell if the user tries to close the window in C++?

I need to break a while loop when the use clicks the close button on the window, but I don't know what to check for. I'm using allegro to run the GUI.
If using Allegro 4: set_close_button_callback()
volatile int hit_closed = 0;
void close_button_proc()
{
hit_closed = 1;
}
// later after creating the display:
set_close_button_callback(close_button_proc);
while (!hit_closed)
{
}
With Allegro 5, it's more like:
al_register_event_source(queue, al_get_display_event_source(display));
// in your event loop:
if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
}
See the manual for all the details.