Skip to content

On Lesson 1, the window does not show up #14

Description

@TonalidadeHidrica

I tried the tutorial Lesson 1. I successfully compiled the file, but when running, it did not show any window; all that happened was that the terminal stopped for exactly three seconds and then the program ended with exit code 0.

I googled and found a similar issue on StackOverflow: SDL Window Not Showing Up at all. According to the answer, it may be the problem that SDL_Delay prevents the window from opening. I tried two source code on the page, one from the question and the other from the answer, then the former does not show the window, while the latter did. So I guess the reason Lesson 1 did not work is the same to that? Maybe it needs to be rewritten.

Also, if you don't mind, please tell me why "SDL_Delay [blocks] the window gets a chance to show"? I'm completely new to SDL, and I want to understand how does it work, what is going on under the hood.

System Information:

$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.14.6
BuildVersion:   18G2022
$ g++ --version
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
$ brew info sdl2
sdl2: stable 2.0.10 (bottled), HEAD
Low-level access to audio, keyboard, mouse, joystick, and graphics
https://www.libsdl.org/
/usr/local/Cellar/sdl2/2.0.10 (87 files, 4.6MB) *
  Poured from bottle on 2019-09-03 at 09:45:53
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/sdl2.rb
==> Options
--HEAD
        Install HEAD version
==> Analytics
install: 53,946 (30 days), 163,725 (90 days), 730,192 (365 days)
install-on-request: 6,346 (30 days), 19,145 (90 days), 82,627 (365 days)
build-error: 0 (30 days)

The source code I wrote, which did not show a window:

#include <iostream>
#include <SDL.h>

int main(int argc, const char **argv){

if (SDL_Init(SDL_INIT_VIDEO) != 0){
	std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
	return 1;
}

SDL_Window *win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN);
if (win == nullptr){
	std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
	SDL_Quit();
	return 1;
}

SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (ren == nullptr){
	SDL_DestroyWindow(win);
	std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
	SDL_Quit();
	return 1;
}

std::string imagePath = "/path/to/hello.bmp";
SDL_Surface *bmp = SDL_LoadBMP(imagePath.c_str());
if (bmp == nullptr){
	SDL_DestroyRenderer(ren);
	SDL_DestroyWindow(win);
	std::cout << "SDL_LoadBMP Error: " << SDL_GetError() << std::endl;
	SDL_Quit();
	return 1;
}

SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, bmp);
SDL_FreeSurface(bmp);
if (tex == nullptr){
	SDL_DestroyRenderer(ren);
	SDL_DestroyWindow(win);
	std::cout << "SDL_CreateTextureFromSurface Error: " << SDL_GetError() << std::endl;
	SDL_Quit();
	return 1;
}

//A sleepy rendering loop, wait for 3 seconds and render and present the screen each time
for (int i = 0; i < 10; ++i){
 	//First clear the renderer
 	SDL_RenderClear(ren);
 	//Draw the texture
 	SDL_RenderCopy(ren, tex, NULL, NULL);
 	//Update the screen
 	SDL_RenderPresent(ren);
	//Take a quick break after all that hard work
	SDL_Delay(1000);
}

SDL_DestroyTexture(tex);
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
SDL_Quit();

	return 0;
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions