I have set up my allegro 5.0.7 project in MSVC 2010 properly and the code executes. I am able to compile and run programs that will display an error dialog or something. However, whenever I run a program that draws a window, the window is not shown on my screen. I see it minimized with a broken file icon. The code runs with no errors, however. Here is an example of some code that gives me this problem. Thanks!
#include <stdio.h>
#include <allegro5/allegro.h>
int main(int argc, char **argv){
ALLEGRO_DISPLAY *display = NULL;
if(!al_init()) {
fprintf(stderr, "failed to initialize allegro!\n");
return -1;
}
display = al_create_display(640, 480);
if(!display) {
fprintf(stderr, "failed to create display!\n");
return -1;
}
al_clear_to_color(al_map_rgb(0,0,0));
al_flip_display();
al_rest(10.0);
al_destroy_display(display);
return 0;
}
This code even exits after 10 seconds, as it should. The only problem is that the window is not drawn to the screen. It is only minimized, with a broken file icon. I have Windows 7 64-bit.
This is a known bug affecting certain configurations that may be fixed in a more recent version.
Use al_set_window_position() to move the window onscreen.
Related
This test program should create a blank window that stays open until you x-it-out. I copied it from SDL's documentation to make sure it is correct. It can be found here.
// Example program:
// Using SDL2 to create an application window
#include "SDL.h"
#include <stdio.h>
int main(int argc, char* argv[]) {
SDL_Window *window; // Declare a pointer
SDL_Init(SDL_INIT_VIDEO); // Initialize SDL2
// Create an application window with the following settings:
window = SDL_CreateWindow(
"An SDL2 window", // window title
SDL_WINDOWPOS_UNDEFINED, // initial x position
SDL_WINDOWPOS_UNDEFINED, // initial y position
640, // width, in pixels
480, // height, in pixels
SDL_WINDOW_OPENGL // flags - see below
);
// Check that the window was successfully created
if (window == NULL) {
// In the case that the window could not be made...
printf("Could not create window: %s\n", SDL_GetError());
return 1;
}
//game loop, quitGame to quit
bool quitGame = false;
//var for checking events
SDL_Event event;
while(!quitGame) {
//Update particles
//Draw particles
//Check for events
while(SDL_PollEvent(&event)) {
if(event.type == SDL_QUIT)
quitGame = true;
}
}
// Close and destroy the window
SDL_DestroyWindow(window);
// Clean up
SDL_Quit();
return 0;
}
It doesn't create a window and terminates immediately, but gives no errors.
I'm using Eclipse, mingw32, and the latest stable release of SDL2. SDL2's libraries and headers are within a file in my C drive. I am using a 64 bit system. I include the entire folder of SDL2's header files. The only library folder I have linked is the one within the 64 bit part of the SDL2 folder. The libraries I have linked are the ones suggested by HolyBlackCat, (in this order) mingw32, SDL2main, and SDL2. Any help is greatly appreciated. Thanks!
I'm new to c++ and XCode, I'm using sdl2 to create a window but when i compile it, it crashes giving me a thread.I have included opengl.h , stdio.h and SDL2.h. There are questions about
dlyd:library not loaded but their different.
Error Message:
dyld: Library not loaded: #rpath/SDL2.framework/Versions/A/SDL2 Referenced from:
/Users/shayanrazavi/Library/Developer/Xcode/DerivedData/c++_code-bbdyozxqxxdxosbxuyhcrqobxrkd/Build/Products/Debug/c++
code
Reason: image not found
This is the code I used i couldn't get int main to be inside the code block for some reason but anyway and I got this code from https://wiki.libsdl.org/SDL_CreateWindow.
int main(int argc, char* argv[]) {
SDL_Window *window; // Declare a pointer
SDL_Init(SDL_INIT_VIDEO); // Initialize SDL2
// Create an application window with the following settings:
window = SDL_CreateWindow(
"An SDL2 window", // window title
SDL_WINDOWPOS_UNDEFINED, // initial x position
SDL_WINDOWPOS_UNDEFINED, // initial y position
640, // width, in pixels
480, // height, in pixels
SDL_WINDOW_OPENGL // flags - see below
);
// Check that the window was successfully made
if (window == NULL) {
// In the event that the window could not be made...
printf("Could not create window: %s\n", SDL_GetError());
return 1;
}
// The window is open: enter program loop (see SDL_PollEvent)
SDL_Delay(3000); // Pause execution for 3000 milliseconds, for example
// Close and destroy the window
SDL_DestroyWindow(window);
// Clean up
SDL_Quit();
return 0;
}
I figured out why this was happening I was meant to put the framework in /Library/Frameworks folder before using it in XCode because when you download SDL it gives you a read me file and the read me file says to put it in that folder.
I should try reading all the text in read me files next time I guess. But if I try running this in XCode it will crash for some reason. (Makes sense because it says dyld: Library not loaded and we just put it in /Library/Frameworks)
My problem is I can't load bitmap to allegro after compiling windows stops working. I tried different bitmaps with different color depths but it still doesn't work.
#include <allegro.h>
int main(int argc, char *argv[])
{
allegro_init();
install_keyboard();
set_color_depth(16);
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0,0);
set_window_title("The Game");
BITMAP *bmp = create_bitmap(800,600);
clear_bitmap(bmp);
BITMAP *BG = load_bitmap("tlotlo.bmp",NULL);
while(!key[KEY_ESC])
{
blit(bmp, screen, 0,0,0,0, bmp->w, bmp->h);
blit(BG, bmp, 0,0,0,0, BG->w, BG->h);
}
destroy_bitmap(bmp);
destroy_bitmap(BG);
return 0;
}
END_OF_MAIN();
Use set_color_depth(desktop_color_depth())
You aren't checking return codes. If create_bitmap or load_bitmap fail, they will return NULL and you'll need to deal with that accordingly.
Loading a bitmap will fail if you aren't in the proper directory. You can test that out by using a full path to the bitmap. If a full path fixes the problem, then you should reconfigure your IDE to work properly.
It's possible that the BMP file is not supported by Allegro. Allegro 4's BMP loading code cannot load more "modern" versions of the format. If you're just getting started, you should really use Allegro 5 instead.
When I want to draw a line by using al_draw_line(100,100,400,400,al_map_rgb(255,255,0),5);, my ALLEGRO_DISPLAY gets closed automatically and statements after this line does not execute. My program just halts
#include<allegro5/allegro.h>
#include<allegro5/allegro_primitives.h>
int main(){
ALLEGRO_DISPLAY *display=NULL;
if(!al_init()){
return -1;
}
if(!al_init_primitives_addon()){
return -1;
}
display=al_create_display(600,600);
al_draw_line(100,100,400,400,al_map_rgb(0,0,0),3);
al_clear_to_color(al_map_rgb(0,0,0));
al_flip_display();
al_rest(20.0);
al_destroy_display(display);
return 0;
}
You don't check for the return value of al_create_display, that can fail. Anyway: I don't know why your program apparently crashes (your code doesn't do anything that should crash, aside from not checking the display) but you won't see anything because you:
A) draw the line and background in the same color (black)
B) clear the background color after drawing the line (thus overdrawing it).
Your code after making some changes:
#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h>
#include <stdio.h>
int main(){
ALLEGRO_DISPLAY *display=NULL;
if(!al_init()){
fprintf(stderr,"Couldn't initialize allegro!\n");
return -1;
}
if(!al_init_primitives_addon()){
fprintf(stderr,"Couldn't initialize primitives addon!\n");
return -1;
}
display=al_create_display(600,600);
if(!display) {
fprintf(stderr,"Couldn't create allegro display!\n");
return -1;
}
al_clear_to_color(al_map_rgb(0,0,0));
al_draw_line(100,100,400,400,al_map_rgb(255,0,0),3);
al_flip_display();
al_rest(1.0);
al_destroy_display(display);
return 0;
}
If it's actually your display that's broken, you should at least get an error message now. This works for me though, and I don't see why it shouldn't for you (unless you try to create this in fullscreen mode, which won't work).
al_draw_line(100,100,400,400,al_map_rgb(0,0,0),3);
al_clear_to_color(al_map_rgb(0,0,0));
These two lines are backwards. You're clearing (wiping) your display AFTER you are drawing your line. So you are never actually seeing the line.
I'd like to have width and height of the currently focussed window. The selection of the window works like a charm whereas the height and width are always returning 1.
#include <X11/Xlib.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
Display *display;
Window focus;
XWindowAttributes attr;
int revert;
display = XOpenDisplay(NULL);
XGetInputFocus(display, &focus, &revert);
XGetWindowAttributes(display, focus, &attr);
printf("[0x%x] %d x %d\n", (unsigned)focus, attr.width, attr.height);
return 0;
}
Is this not the "real" window but the currently active component (like a textbox or a button?) And why would it have the size of 1x1 anyways then? If this is the case, how do i get the main window of the application containig this control? Means... kinda the top-level window, the top-most window except the root window.
PS: Don't know whether it's really important; I use Ubuntu 10.04 32 and 64 bit.
You're right - you're seeing a child window. GTK applications, in particular, create a child window under the "real" window, which is always 1x1, and that always gets the focus when the application has the focus. If you're just running your program using the GNOME terminal, you'll always be seeing a GTK application with the focus (the terminal).
If you run your program in such a way that a non-GTK program happens to have the focus, then this doesn't happen, but you could still end up finding a child window with the focus instead of the top-level window. (One way of doing this is to run sleep before your program like this: sleep 4; ./my_program - this gives you a chance to change the focus.)
To find the top-level window, I think XQueryTree will help - it returns the parent window.
This worked for me:
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
/*
Returns the parent window of "window" (i.e. the ancestor of window
that is a direct child of the root, or window itself if it is a direct child).
If window is the root window, returns window.
*/
Window get_toplevel_parent(Display * display, Window window)
{
Window parent;
Window root;
Window * children;
unsigned int num_children;
while (1) {
if (0 == XQueryTree(display, window, &root,
&parent, &children, &num_children)) {
fprintf(stderr, "XQueryTree error\n");
abort(); //change to whatever error handling you prefer
}
if (children) { //must test for null
XFree(children);
}
if (window == root || parent == root) {
return window;
}
else {
window = parent;
}
}
}
int main(int argc, char *argv[])
{
Display *display;
Window focus, toplevel_parent_of_focus;
XWindowAttributes attr;
int revert;
display = XOpenDisplay(NULL);
XGetInputFocus(display, &focus, &revert);
toplevel_parent_of_focus = get_toplevel_parent(display, focus);
XGetWindowAttributes(display, toplevel_parent_of_focus, &attr);
printf("[0x%x] %d x %d\n", (unsigned)toplevel_parent_of_focus,
attr.width, attr.height);
return 0;
}