I know this question has been asked before but most of the time answer was just to add delay or an event loop. However I have added an event loop and the window ist not showing. Only the console. I am running this program in Visual Studio 2019.
#include <iostream>
#include "GL/glew.h"
#define SDL_MAIN_HANDLED
#include "SDL.h"
int main() {
SDL_Window* window;
SDL_Init(SDL_INIT_EVERYTHING);
//fenster erstellen
window = SDL_CreateWindow("C++ OpenGL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_OPENGL);
//opengl context setzen
SDL_GLContext glContext = SDL_GL_CreateContext(window);
bool close = false;
while (!close) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
close = true;
}
}
if (close) {
break;
}
}
return 0;
}
You need to include SDL_MainReady as you are not using SDL_main.
See here
So you code would be adjusted like
int main() {
SDL_Window* window;
SDL_SetMainReady();
SDL_Init(SDL_INIT_EVERYTHING);
...
return 0;
}
Related
A am trying to compile a simple bgfx program (from this link) and failing, because the window is not updating. Closing the window is fine, so i think the problem is in bgfx. I also tried other tutorials and those didn't work either
Code:
#include <stdio.h>
#include <bgfx/bgfx.h>
#include <bgfx/platform.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_syswm.h>
SDL_Window* window = NULL;
const int WIDTH = 640;
const int HEIGHT = 480;
int main (int argc, char* args[]) {
// Initialize SDL systems
if(SDL_Init( SDL_INIT_VIDEO ) < 0) {
printf("SDL could not initialize! SDL_Error: %s\n",
SDL_GetError());
}
else {
//Create a window
window = SDL_CreateWindow("BGFX Tutorial",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
WIDTH, HEIGHT,
SDL_WINDOW_SHOWN);
if(window == NULL) {
printf("Window could not be created! SDL_Error: %s\n",
SDL_GetError());
}
}
// Collect information about the window from SDL
SDL_SysWMinfo wmi;
SDL_VERSION(&wmi.version);
if (!SDL_GetWindowWMInfo(window, &wmi)) {
return 1;
}
bgfx::PlatformData pd;
// and give the pointer to the window to pd
pd.ndt = wmi.info.x11.display;
pd.nwh = (void*)(uintptr_t)wmi.info.x11.window;
// Tell bgfx about the platform and window
bgfx::setPlatformData(pd);
// Render an empty frame
bgfx::renderFrame();
// Initialize bgfx
bgfx::init();
// Reset window
bgfx::reset(WIDTH, HEIGHT, BGFX_RESET_VSYNC);
// Enable debug text.
bgfx::setDebug(BGFX_DEBUG_TEXT /*| BGFX_DEBUG_STATS*/);
// Set view rectangle for 0th view
bgfx::setViewRect(0, 0, 0, uint16_t(WIDTH), uint16_t(HEIGHT));
// Clear the view rect
bgfx::setViewClear(0,
BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH,
0x443355FF, 1.0f, 0);
// Set empty primitive on screen
bgfx::touch(0);
// Poll for events and wait till user closes window
bool quit = false;
SDL_Event currentEvent;
while(!quit) {
while(SDL_PollEvent(¤tEvent) != 0) {
if(currentEvent.type == SDL_QUIT) {
quit = true;
}
}
}
// Free up window
SDL_DestroyWindow(window);
// Shutdown SDL
SDL_Quit();
return 0;
}
Why is this happening, and how to fix it?
P.S.: I am using kali linux
From this Lazy Foo tutorial (https://lazyfoo.net/tutorials/SDL/21_sound_effects_and_music/index.php) I wrote the following lines of code:
#include <SDL.h>
#include <SDL_mixer.h>
bool running = true;
int main(int argc, char** argv) {
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
SDL_Window* window = SDL_CreateWindow("testing musique", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, 0);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_Event quit;
Mix_Music* music;
while (running) {
while(SDL_PollEvent(&quit)){
switch(quit.type) {
case SDL_QUIT:
running = false;
break;
}
}
Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048);
music = Mix_LoadMUS("../pikachu/keypress_BMP/beat.wav");
Mix_PlayMusic(music, -1);
SDL_SetRenderDrawColor(renderer, 20, 20, 255, 255);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
}
Mix_FreeMusic(music);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
I even used the same audio file (beat.wav) as Lazy Foo did in his tutorial, but while his program runs without a hitch, mine plays the wav too fast (despite the fact that I checked every parameters to make sure mine matched with his). I tried decreasing the frequency parameter in Mix_OpenAudio, but while the wav did slow down, so did the pitch, and it should not have made sense to do this in the first place. What should I do?
The audio subsystem should only be opened once, before the main loop, meanwhile you open it in each iteration of the loop. The same goes for loading the file into memory — again on each iteration of the loop, instead of once. So the correct code structure should look like this:
#include <SDL.h>
#include <SDL_mixer.h>
int main(int argc, char** argv) {
bool running = true;
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
SDL_Window* window = SDL_CreateWindow("testing musique", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, 0);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
// open audio and load music file before main loop
Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048);
Mix_Music* music = Mix_LoadMUS("../pikachu/keypress_BMP/beat.wav");
SDL_Event event;
while (running) {
while(SDL_PollEvent(&event)){
switch(event.type) {
case SDL_KEYDOWN:
switch(event.key.keysym.sym) {
case SDLK_P:
Mix_PlayMusic(music, -1);
break;
case SDLK_S:
Mix_HaltMusic();
break;
}
case SDL_QUIT:
running = false;
break;
}
}
SDL_SetRenderDrawColor(renderer, 20, 20, 255, 255);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
}
// close audio and free the music after main loop
Mix_CloseAudio();
Mix_FreeMusic(music);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
Don't forget about Mix_CloseAudio and about error checking.
I am 100% sure I have set the SDL library to work properly
#include <iostream>
#include <SDL.h>
using namespace std;
int main(int argc, char* argv\[\]) {
// Initializing SDL
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window *window = 0;
// Creating the window
window = SDL_CreateWindow("Hello World", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, 640, 480,SDL_WINDOW_SHOWN);
// Just so I can see the window because it goes away immediately.
SDL_Delay(5000);
SDL_Quit();
return 0;
}
After you create the window, you need to handle events using SDL_PollEvent. Instead of SDL_Delay(5000), do something like this:
// ... setup (SDL_Init, SDL_CreateWindow, etc.)
SDL_Event event;
for(;;) {
while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_QUIT:
goto done;
}
}
}
done:
SDL_Quit();
return 0;
I am writing a simple SDL2 application with 2 windows.
The first window (window variable) is shown when the application starts, the second one (window2 variable) is hidden.
Expected behavior:
I click on the first window, the second window pops up, then I close the second window.
And I can close and reopen the window as much as I want.
Observed behavior:
Once I close the second window, if I reclick in the first window, the second window doesn't appear as expected.
As stated in my comment: the window doesn't appear in my window manager (i.e. Wayland).
The code:
#include <SDL2/SDL.h>
int main()
{
SDL_Window* window, *window2 = NULL;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
return 1;
} else {
window = SDL_CreateWindow("ONE", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
window2 = SDL_CreateWindow("TWO", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, 320, 240, SDL_WINDOW_HIDDEN);
if (window == NULL || window2 == NULL) {
SDL_DestroyWindow(window);
SDL_DestroyWindow(window2);
return 1;
}
bool running = true;
while(running) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_WINDOWEVENT) {
if (event.window.event == SDL_WINDOWEVENT_CLOSE) {
if (SDL_GetWindowID(window) == event.window.windowID) {
running = false;
} else {
SDL_HideWindow(window2);
}
}
} else if (event.type == SDL_MOUSEBUTTONDOWN) {
SDL_ShowWindow(window2);
}
}
}
}
SDL_DestroyWindow(window);
SDL_DestroyWindow(window2);
SDL_Quit();
return 0;
}
This is an SDL bug that may or may not have been fixed by this patch.
You should call SDL_RaiseWindow to place the second window on top of the other.
From lazyfoo's legendary SDL tutorials:
void LWindow::focus()
{
//Restore window if needed
if( !mShown )
SDL_ShowWindow( mWindow );
//Move window forward
SDL_RaiseWindow( mWindow );
}
I am just now learning SDL and have downloaded the libraries and added them to my linker, etc with MinGW and I am trying to run a simple demo program to display a window and it will not show up at all. I get no errors at all, the window just doesn't show up.
#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;
}
// The window is open: could enter program loop here (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 just tested this on Linux and MinGW. It may be a problem with SDL_Delay blocking before the window gets a chance to show. Try adding a basic main loop to see if it works. This will create an empty 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;
}
// A basic main loop to prevent blocking
bool is_running = true;
SDL_Event event;
while (is_running) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
is_running = false;
}
}
SDL_Delay(16);
}
// Close and destroy the window
SDL_DestroyWindow(window);
// Clean up
SDL_Quit();
return 0;
}