SDL Window won't pop up - c++

My window won't pop up.
Console appears, and without opening window just says that "Application ended"
Here is my code:
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_image.h>
#include <windows.h>
using namespace std;
SDL_Window * okno;
SDL_Surface * ekran;
SDL_Event zdarzenie;
int frame = 0;
int main(int argc, char*args[])
{
SDL_Init(SDL_INIT_EVERYTHING);
return 0;
okno = SDL_CreateWindow("SDL_TEST", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, NULL);
ekran = SDL_GetWindowSurface(okno);
}

return 0; is your bug. After the return the program ends because main() ends. No lines in main are executed after the return. You certainly did not want to end before you called SDL_CreateWindow.
Can a function continue after a return statement?
Change the code to
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_image.h>
#include <windows.h>
using namespace std;
SDL_Window * okno;
SDL_Surface * ekran;
SDL_Event zdarzenie;
int frame = 0;
int main(int argc, char*args[])
{
SDL_Init(SDL_INIT_EVERYTHING);
okno = SDL_CreateWindow("SDL_TEST", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, NULL);
ekran = SDL_GetWindowSurface(okno);
}
In c++ return 0; can be omitted from main.
Can I omit return from main in C?
Also I would expect this code to issue a warning (about unreachable code) on many compilers. If it did not warn you may need to turn up the warning level of your compiler. If it did warn you need to pay more attention to the warnings..
You may also want to remove the global variables and instead make your variables local to main. Global variables are usually considered a bad practice.
Are global variables bad?
Also SDL_Init() can fail. You may want to check its return value and quit on failure logging the error.
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
return 1;
}
https://wiki.libsdl.org/SDL_Init

Related

SFML 2.0 - Window closes immediately after showing up

Window immediately closes after showing up, even though it should wait for close event.
main.h:
#include <SFML/Graphics.hpp>
#include <iostream>
#include "window.h"
int main()
{
window::window mainwindow;
mainwindow.createwindow(800, 600);
while(mainwindow.window.pollEvent(mainwindow.events)){
if (mainwindow.events.type == sf::Event::Closed){
mainwindow.window.close();
}
}
return 0;
}
window.h:
#include <SFML/Graphics.hpp>
#include <iostream>
namespace window{
class window{
public:
sf::Window window;
sf::Event events;
void createwindow(int resx, int resy){
window.create(sf::VideoMode(resx, resy), "MainWindow", sf::Style::Default);
}
};
}
The code is based on this SFML tutorial : https://www.sfml-dev.org/tutorials/2.5/window-window.php
I am using SMFL 2-2.5.1-23.83 with IDE being code::blocks 20.03.
Thanks for all help

LNK1561 entry point must be defined - SDL

I got a LNK1561 entry point must be defined error i tried somethings my self as stsyem settings to console and it still doesn't work. Here is my code for every class the SDl.h is from the SDL.h donwload page.
main.cpp:
#include <iostream>
#include "MainGame.h"
int main(int argc, char** argv) {
std::cout << "Enter any ket to quit...";
int a;
std::cin >> a;
return 0;
}
MainGame.cpp:
#include "MainGame.h"
MainGame::MainGame()
{
_window = nullptr;
_screenHeight = 1028;
_screenWidth = 768;
}
MainGame::~MainGame()
{
}
void MainGame::run() {
InitSystems();
}
void MainGame::InitSystems() {
SDL_Init(SDL_INIT_EVERYTHING);
_window = SDL_CreateWindow("title", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, 1028, 768, SDL_WINDOW_OPENGL);
}
MainGame.h:
#pragma once
#include <SDL/SDL.h>
class MainGame
{
public:
MainGame();
~MainGame();
void run();
void InitSystems();
private:
SDL_Window* _window;
int _screenWidth;
int _screenHeight;
};
Allt his code is to open an Windowed frame on your computer en open an console with the text Press Any ket to quit... If i remove the SDL.h include and the SDL code it all works if i put the include back and not the SDL code it gives the error again.
Have you tried using MainGame in your main function? Chances that the compiler assumes that it is never used and you do not get the #include <SDL/SDL.h> directive to work. Also, consider changing #include <SDL/SDL.h> to #include "SDL/SDL.h".
Also you might consider using SDL_SetMainReady for the cases that you are not using SDL_Main as the entry point.

SDL2 OpenGL Window Instantly closes

I am trying to write a basic game using OpenGL and SDL2 but whenever I run the program the window immediately closes
Window.cpp
#include "Window.h"
#include <GL/glew.h>
Window::Window(const char* title)
{
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 900, 900, SDL_WINDOW_OPENGL);
context = SDL_GL_CreateContext(window);
GLenum status = glewInit();
}
Window::~Window()
{
SDL_DestroyWindow(window);
SDL_GL_DeleteContext(context);
SDL_Quit();
}
void Window::Input()
{
SDL_Event e;
while (true)
{
if (e.type = SDL_QUIT)
{
exit(0);
}
}
}
void Window::Update()
{
SDL_GL_SwapWindow(window);
Input();
}
Window.h
#pragma once
#include <SDL.h>
#include <GL/glew.h>
class Window
{
SDL_Window* window;
SDL_GLContext context;
public:
void Input();
void Update();
Window(const char* title);
~Window();
};
Main.cpp
#include <SDL.h>
#include <GL\glew.h>
#include "Window.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
Window window("Window");
while (true)
{
glClearColor(0, 1, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
window.Update();
}
return 0;
}
When I run the code I get a brief flash of a green window then it immediately crashes. when I remove Input(); from my update function it the window does not crash but it is unresponsive. I have tried to change SDL_PollEVent to SDL_WaitEvent and adding delays to the Input function but nothing works
First thing, you are using an assignment operator when you probably want to check for equivalence:
if (e.type = SDL_QUIT)
Should be:
if (e.type == SDL_QUIT)
Additionally, you have other issues. You declare the SDL_Event e; union just before you test it, but you don't initialise it to any value. Then you go on to loop over that variable waiting for it to be set to quit. Nothing can change the value of that variable, so how will your loop ever exit?

Why _kbhit() freeze the thread?

I want to know why _kbhit() in loop doesnt allow to work thread, in a code below there must be an output to the screen "Test!", but it doesnt happen. Why?
#include <iostream>
#include <process.h>
#include <conio.h>
using namespace std;
void Thread(void *par)
{
while (true)
{
cout<<"Test!"<<endl;
};
}
int main(int argc, char* argv[])
{
_beginthread( Thread, 0, NULL);
while(!_kbhit());
return 0;
}

Window refuses to render

I recently got started trying to use SFML. For some reason my simple program will not render the window. I've tried throwing everything into main to see if there was an error in my code that had to do with multiple files etc. but to no avail.
I'll launch my program and nothing will appear.
What's the problem?
//main.h
#ifndef MAIN_H
#define MAIN_H
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <iostream>
#include <fstream>
using namespace std;
using namespace sf;
class game
{
public:
void startLoop(int SCREEN_W, int SCREEN_H, string SCREEN_NAME);
void log(const string logging);
game()
{
QUIT = false;
pendingFile.open("Log.txt", ios::out);
pendingFile << "---Brain Bread Log---";
}
~game()
{
pendingFile.close();
}
private:
bool QUIT;
ofstream pendingFile;
};
#endif
//main.cpp
#include "main.h"
void game::log(const string logging)
{
pendingFile << logging;
}
void game::startLoop(int SCREEN_W, int SCREEN_H, string SCREEN_NAME)
{
Window Game(VideoMode(SCREEN_W, SCREEN_H, 32), SCREEN_NAME);
while(QUIT == false)
{
Game.Display();
}
}
int main(int argc, char* argv[])
{
game gameObj;
gameObj.startLoop(800, 600, "Brain Bread");
return 0;
}
I tried your code and it behaves exactly as I expect it to - that is to say that an iconless window with a black body pops up and it doesn't respond to events. Is that what you're getting? If not, you might need to rebuild SFML.
You might want to try introducing event-handling so that your startLoop looks more like this:
void game::startLoop(int SCREEN_W, int SCREEN_H, string SCREEN_NAME)
{
// Init stuff
while (Game.IsOpened())
{
sf::Event newEvent;
while (Game.GetEvent(newEvent))
{
// Process event
}
// Do graphics stuff
Game.Display();
}
}