23
Aug

Making A Game #1: Making The Game Loop : C++ And SDL2 Tutorial


Hi, it’s carl. Let’s make games here. Let’s make the game loop so we can start a new project And we’ll call this 2d engine because that’s what it’s going to be it’s going to be start of our 2D game engine 2d game engine next Right once you create a new project you [need] to get SDL installed in your project links are in the description for how to do that for xcode and Visual Studio, and so I’m just going to quickly skip ahead you can watch the other videos if you need to watch that again So there we go. [it’s] still fine work, and we have our main function. We’re going [to] clear all [that] out Don’t need any of [that] right The whole idea of the game loop? Is it continuously? goes over everything we need to do so while Game is running We will Handle any user input Then we [walk] update all objects for exact for example positions ETC, ooh We don’t to be doing that positions Then we’ll render changes to the Display The whole idea is that we do this over and over again? Until the games not running anymore and we’ve quit all the characters died, so this is what we can implement So that’s my games All right, so first thing is we need to create a game class So great new C++. File like a cool game right So in the game header We are going to include [Stl]. So we’re going to need to [know] which is going to be responsible [for] initializing that for us Windows users won’t need this here. You’ll just be that, but if you’re on a mac Then you’ll need that right, so In our game class definition we’re going to define our game class, so class game curly braces with seMicolon on the end Right in here. We can have a public members and private members Right so things that can be publicly available to other classes from here They’re going to be the constructor which [we’re] [going] [to] use to construct the game object Deconstructor for when we close it right And as we said this [classes] Gotta be responsible Updating [and] Stl and everything else so what we’re going to have We’re going to have a method for initializing it And we’re going to have this taking Kant’s Char star which is basically a string then called title we want int [x] [position] And it in y position Then we can have it in width and also in inked for height Well one thing I’m going to do is also taking a ball called Fullscreen This allows to easily flag whether we want the game to be fullscreen or not when we create the game All right after we’ve initialized we’re going to need to an update function this will list go through all our game objects and update them all we’ll need a void [renderer] and [I] have to update Brenda. We need to go to clean You do some memory management for us [ok] the game objects from memory once you’re finished with them We’re also going to need to handle events if we put them in order. We’re going to do them So we can initialize this then it will be handling events so avoid handle events right now To let the main function know that the game should still be running We’ll create another function called void running Now this will return Boolean absolute so ball running and then when in our wallet when we initialize it we can ask the game class are you running and If it’s true, then [it] will run the loop again, so we’ll handle the reds will update render So for all this to work our private variables are going to need a ball saying call it is running and then also we need an spL window and renderer, so [Sdl] window Called Window and a [SDl] Renderer which [we’ll] call renderer Right now we have all these what we’re going to do Is Define them all? Well, we’ve already [define] them So this is everything that our game class at this moment. They’re going to be responsible for we now Need to do the source code for each of these so in the game cPP file We are going to create game D constructor and constructor So we’ve got that Then we also need to Declare in it Which basically we’re just going to copy this out, so I? can just double click these and [that’s] xcode. Look not sure if you can in visual studio but If not, just type them out like that So we’re going to initialize our [SDL] We can have our update function This is where we would have the game update all these positions. We have a void render Are we going to have handle events apparently so void? game class Handle Events And Er, and we’ve also got a clean function. So void game clean So we’ve got one two three four five six seven functions. Make sure we’ve declared them or [note] we’ve missed running. So we’re going to do that Up today what we’ll do we running will just make it in line. So we’ll just make this return is running and The stuff done, [so] one two three [four] five six seven right? Now let’s add some code please Alright So let’s start with SDl right, so Firstly when we initialize this deal, we want to make sure that it initializes properly didn’t create any errors before we start creating windows And renderers so what can go is if we’re going to call SDL in it as we did before in the previous videos And we’re going to put in SDl to init everything Now if that is equal [to] 0 then we have successfully Initialized the SDL subsystems, so what we’re going to do We’re going to add some output to the console so you can see what’s going on behind the scenes [so] we’re just going to print out subsystems initialized And do that And see out not sorry Not see out end line Alright, that’s done now With Sdi initialized we’re going to create the window. So we’re going to take our window that we declared in our header file And we’re going to go [STl] Create window now. This is where we put in the title the exhibition the white petition the width height and Full screen which we can’t pass in it takes you in 32 flags, so we’ll deal with that one in a second So we’re going to [put] in title I’m going to get x position y [cost] and width and height which go with Hiked [right] four flags. We’ll leave that for moment what we can do Will create an int called flags equal to [zero]? And in here where it wants flags, we will pass in flags so if we want fullscreen we can adjust this flags value like such so if Fullscreen is true we can set flag equal to Sdl window full screen So when we initialize this game? We can [sort] [of] review like to be [fullscreen] or not just by passing your true or false into the argument Makes it nice and easy for us through with that line Right this job right there we go [and] last now I’ve got a problem here And declared use of that, so we don’t have a oh stream Okay, so I’m gonna change that there to iostream That’ll fix that problem All right now. We’ve got the standard there. We go. So that’s done then we’re going to crack the window Then we’re going to check make sure the windows been done properly so if window True [basically] anything other than zero then it means. It’s [being] created so we can output so see out window created and then n glance What system? We can now create the renderer so our Endre It’s going to be equal to STl [crike] Renderer and we going to pass in our window. We’ve just created and passing it index and flags and We can do the same make sure the renderer or let us know if the render has been created successfully or not And we’ll just copy this line here And [paste] that in there Change up to render Render is created if this Has all worked what we’re going to do is make is running equal to true otherwise so else We will set is running to False So even if Sdl doesn’t initialize correctly we all have brilliant is running set to False So we check for it in the main Loop see whether we should carry on running the game it will see that. It’s false and it [webber] in the game So [that’ll] be sdl. Initialized for us Next [thing] we wanted to do is handle any events so what we’re going to do first For this one. We are just going to look up for the event [SDl] quit I’m Sdm. Okay event. [we’ll] call it event Then we can pull that event with SDL [pull] event pass The reference event because it won’t so know exactly where it is Now what we can do is run a switch statement on this and find out what type of event it is So what we’re going to do is switch And this expression we get a check for event type Now if that event type is equal to sel quit – there we are going to set is running to False So when we click the x on the window will generate an event called SDl quit? which will then get found by [our] handle events and Stop the game from running on its next loop right One thing we want to do now is the render This is where we render our objects screen so First thing to do is to clear what’s in the renders buffer render as buffer so we can go SDl? render clear and passing the renderer And this is where we would add stuff to render and then we present Going to present render up Once we’ve done that When we [quit] the game we want to clean up after ourselves, so we’re going to go SDl and [note] Destroy, [I’m] going to Destroy the window passing our window And we’re also going to destroy our renderer Now we’re going to call [SDl]. Quit if you’ll close off all the subsystems, and then we will also put Will see out? game cleaned Alright one thing to do now if we want we’ve created the renderer In here before we render a created. We can you set the render draw color so Stl Set render draw color. [I’m] going to set this to white so I’m going [to] pick the render up So there we go now have that Update render clean so now we’ve implemented Start of our game class We can start using this in the main like now to create our game loop so in our main cPP file We’re going to make sure we include our game header file and First [all] we need to create an instance [of] game, so we’re going to create a game called Let’s make it a pointer game When we make that equal to a null pointer for now and In our main function. This is where we’ll declare it so a good game is equal to a new game Now for the game loop so not not much to it so [boil] now The condition is going to be if the game [2] is running So if the guys are [running] we’ll run our statements in which case, we’ll be Gay you want to handle our events? I’m going to go game We’re going to update our objects And your game and we’re going to go Render bought our objects to the scene now [we] will be stuck in this as long as running is equal to equal to true when it becomes False We’re going to quit a game which means we need to run game clean That’s it. That’s our game loop So press plane out without our game window game cleaned program ended with exit Code 0 That’s right because we didn’t initialize SDl. So is running never got to true So let’s initialize now so game I’m going to call that an init function now [this] [is] where we can put in our title x-position y-position With height and wherever we want the window to be so we call this I’m just going to call it virtue engine Opposition on screen I’m going to use Stl window Pause centered you just give us the center of the window, and that’s the size again here SDl Window pass centered I’ll reach the height on a via 800 wide by 600 hi door walk Fullscreen not at this moment Right now when we run it. We should get a game, so [we] [all] succeeded right there’s our game screen and You can see here we initiated Initialized [SDl] we’ve created our window and the renders kraid now. This is them pretty boring. You can’t really tell if [anything’s] working even though I assure you it is One way to test this Is to put something in our update loop that we can watch so we’re going to quit this And you can see the game it gets clean once we quit it now game update is where we handle? Or our game logic and things like that, so if we go into game In what we can do, we’re going to create a local variable No, we’re not yeah, so what we can do in Private just from to watch we’re going to put in a counter, so we call it in count And we’ll just call it that so What we can do now in our game and update We can basically get count plus plus Which basically every time this gets updated we can increment our counter by one? So what we can do so you can see this work ringo standard, and we’re just going to see out whatever the counter is and And I’m just going to initialize up to zero in here because I’ll be just some random variable So we’re going to do that and we’ll hit play now [what] we’ll see in our console. You can see that Counter is getting updated So what we’ve actually got happening here is in our main function We’ve initialized our window Now it’s true at the moment [handling] our events which we’re not doing anything at the moment [thing] calling games update function, which is here so the plan is from here is not really to have all the game logic in this update function but for This update function to have all the game objects We’re going to have so the player will have its own update function, and we’ll just call the players update function from here And we can keep it all separate and what we can do is from here, we can just exit that off and We’ve clean the game so Let’s try Make it Fullscreen So it’s going to do the same thing so in main when we start it which can set this value here to true And when we were in the game, we’ll have a full screen window Which here’s our window Now it’s full screen. We don’t have any controls So you’ll have to [use] alt [and] f4 to quit out or Mac command q will do that here [we] go we have our game loop which is working Next episode what we’re going to do is bring in an image and start [moving] it around the screen See you next week

Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

100 Comments

  • Fabian Roland says:

    I'd really like to use C# or Java (started with AS3) but the Garbage Collecting stuff is just triggering me so hard
    What kind of practice is it to create stuff and leave it around until someone else ends up picking it up.. It's like leaving plates in the kitchen until someone needing it cleans it up (deleting all references to it is like not leaving leftovers, it doesn't change much)

  • Giri Prahasta Putra says:

    Thank you so much for really concise and step by step API explanation of SDL. It really help me a lot! Subscribed and would watch for more!

  • Chris Mcpherson says:

    Just so you know, there is a line of code that conflicts with Visual Studio

    int main(int argc, const char *argv[])
    This gives me a linked error, where removing const from it makes it work

  • Alec Dosier says:

    For the longest time I could not figure out how to separate SDL into a game engine without the entire project falling apart.
    Thank you for helping me break through this barrier and into much more complex game designs!

  • Fukiyel says:

    Hi, thank you really much for your tutorial ! Is it normal that when i run the program, it takes about 40 seconds to initialize SDL2 ? (By that i mean nothing, not even "system initialized" appears on the console before these 40 seconds)

    I am on Windows 10 using CodeBlocks ^^

  • Silvio M. says:

    shouldn't the game variable be deleted before closing the game? since it is on the heap

  • Michael Brenneman says:

    IDK about mac, but windows needs: int main(int argc, char * argv[]){} as opposed to int main(int argc, const char * argv[]){} …. this was giving me a headache trying to figure out why it kept telling me 'undefined reference to SDL_main'

  • Toaster Cardozo says:

    Hi, I'm trying to follow this tutorial which I am enjoying so far, but when I run my code the window only appears for a second or so and then instantly disappears. I have everything copied more or less exactly (I'm on windows so I skilled the 'SDL2/' in the #include). I feel like this is a problem, is this something anyone else has seen and is there an easy fix?

  • Samuel Karabetian says:

    Thanks for the tutorial!
    Isn't it better to call DestroyRenderer() before DestroyWindow()?
    ..So the things are destroyed in the opposite order they were created.

  • Joris KBos says:

    Why do you make the game object a pointer?

  • GoT Singing says:

    Hello ! I'm always getting LNK2019 unresolved external symbol SDL_main referenced in function main_utf8

    Doing a project in school and really have to solve this .

    Thank you for Amazing tutorials either way

  • Calvin Millett says:

    Hey, I'm having an issue where every time I try to run the program I get an error saying that the application was unable to start (I'm on windows). Here's the full error code: The application was unable to start correctly (0xc000007b). Click ok to close the application. Any help would be much appreciated!

  • VensesladoASMR says:

    Wtf I didn't know there was an IDE for Java on Mac, does it work with OS X on Macbook Pro?

  • VensesladoASMR says:

    I'm following along and I can't figure out how you defined renderer, window, and isRunning. I included Game.h in my Game.cpp. Is this an SDL issue? I just added SDL to my visual studios project.

  • VensesladoASMR says:

    Couldn't wait to bother you again. Is this an error due to not setting up SDL properly? Because I did it like 3 times.
    Error LNK2019 unresolved external symbol _SDL_main referenced in function _main_utf8 GameSDL2main.lib(SDL_windows_main.obj)

  • Wyatt Townsend says:

    I keep on getting told that 'SDL2/SDL.h' file not found, please help me.

  • Trevor Williams says:

    Hello. If you gave or are giving this video a thumbs down, say why. Not enough detail? Bad code? I'm curious.

  • Cloud Boys Music says:

    int main(int argc, char *args[])

    if y'all having trouble.

  • Logan says:

    Good tutorial but painstakingly boring

  • solidstatejake says:

    Why are we defining the Game class in a separate file? I don't understand the benefit to that. Also, my SDL header is not being recognized by my Game.cpp/Game.hpp files. I am using visual studio 2017 professional. Aside from this infuriating problem, I'm loving your work. I really appreciate what you are doing. Thank you.

  • Chris Hoy says:

    I got a weird linker error that I fixed by adding #undef main to the Game.hpp just as a heads up

  • LC says:

    It says external symbol _SDL_main without solvin to what it references at in the function _main_utf8. Any ideas of how to solve it?

  • RemoteBeef says:

    Were you writing out the things you were going to do like; "While(game is running)" so then you can replace those with the code in the future? Genius!

  • Salvatore Campeggio says:

    I understand absolutely nothing, but fuck,subscribed.
    I only use a little c++ at school, but we haven't even did classes.

  • Timon Paßlick says:

    Why is Game a class instead of a namespace?
    You could replace the destructor with a clean_up function.
    And you could replace the constructor with an adapter function which returns a pointer to SDL_Window.

    Btw: I usually give upvotes around once a week, but you deserve one.

  • Check me out ! says:

    omg watching this vid made me ralize what the "->'' stands for when before I couldnt find an understandabe example because I am not from america or england. +1 like

  • no name says:

    For use in linux qtcreator (for my case is centos 7 x64) install SDL2* and SDL2-devel* packages and edit project.pro file:

    TEMPLATE = app
    CONFIG += console
    QMAKE_CXXFLAGS += -std=c++11
    CONFIG -= app_bundle
    CONFIG -= qt
    win32 {
    INCLUDEPATH += ../ThirdParty/windows/include
    LIBS += -LC:../ThirdParty/windows/lib -lSDL2 -lSDL2_image -lSDL2_ttf
    }
    unix:!macx {
    INCLUDEPATH += /usr/local/include/SDL2
    LIBS += -LC:/usr/local/lib64 -lSDL2 -lSDL2_image -lSDL2_ttf
    }
    LIBS += -LC:/usr/lib64 -lm
    SOURCES += main.cpp
    game.cpp

    HEADERS +=
    game.h

  • Gustavo Salmerón says:

    Wow that's a lot of code to build a program! Just a personal opinion, but I tried to learn C++ and, still, classes seem overcomplicated for me. I'd rather use good o'l functions and structs.

    If there are any compensations for using OOP, please tell me, there must be a reason for everybody using it that I still didn't get it.

  • Liam says:

    Hi, got an error for "entry point must be defined error "
    any idea how to fix?

  • Thong Nguyen says:

    Could anyone please help me to explain why, in the video, we dynamically allocated memory using new, to create a pointer to the Game object? Can we just declare a Game variable inside main and use it as a local variable? (I have the same question for other pointers to class objects he declare in later tutorials). Thanks for helping!

  • NEB ORIPAHS says:

    My window closes instantly, when executing the program!

  • Antonio Cs says:

    Why do you use pointers? Why not just create the variable Game on the stack? Is there any benefit by it being on the heap?

  • Hyper System says:

    Hey man!.. I'm finally able to follow along and understand the video haha.

    Got a couple of questions though…
    Why do you make the game object a pointer?
    Is there any downsides of not doing so?

    Much thanks, dude!.. Very helpful stuff.

  • Roane Williams says:

    I got an error when creating a new game instance as a 'nullptr' (which is a keyword in C++11). So it wouldn't allow me to declare it as such.

    So I just used the following instead and it worked fine:
    Game *game = new Game();

    Hope this helps.

  • Jimi Mimo says:

    fullscreen did not work… it showed the 800 by 600 window, without buttons to minimize or exit.

  • Deadlock UK says:

    Great video. Great speed and easy to follow along! Sub and going for the next vid! 🙂 Keep up the great work!

  • 4D. Walkan says:

    awesome dude

  • Cody Caro says:

    This is helping a lot! You got a sub!

  • Harry Wright says:

    Really enjoying this series so far, great stuff!

  • charlie brownau says:

    Gday

    Do we really need another 2D single core optmised game engine ?

  • Vali Zeth says:

    Why do you use the clean function instead of the destructor?

  • Thesh Rk says:

    do we need to use delete() to delete new ?

  • Zamachi says:

    I started doing this in Visual Studio and straight from the start I'm confused about making a new source file; it only creates a "Name".cpp but no .hpp. It might be because of the IDE, but I'm not sure, it's my first time making a game…

    Also, I've been following another tutorial but it only goes so far as creating a game window with a picture on it(I guess I made it using SDL libraries, but I'm unsure), nothing besides it, now I do know C++(I do not know SDL tho), and I have 0 clue of how to start making my game. Would you suggest I start from scratch following your tutorial or I could keep the other code? Here's the code if you're interested(ignore the comments, it's for me to understand what I did):

    #include <iostream>
    #include <SDL.h>
    #include <SDL_image.h>
    const int WIDTH = 800, HEIGHT = 600;
    //WIDTH i HEIGHT imaju dodeljene vrednosti koje predstavljaju
    //sirinu i duzinu prozora
    int main(int argc, char *argv[])//parametri su neophodni zbog SDL-a
    {
    SDL_Surface *imageSurface = NULL;
    //kreiraju se SDL surface(povrsine)
    SDL_Surface *windowSurface = NULL; //ovo ce biti povrsina prozora koji smo ovde kreairali(prozor je kao platno)

    if (SDL_Init(SDL_INIT_EVERYTHING) < 0) //ispituje se inicijalizacija svega u SDL-u
    {
    std::cout << "SDL could not initialize! SDL Error: " << SDL_GetError() << std::endl;

    }
    SDL_Window *window = SDL_CreateWindow("Hello SDL World", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_ALLOW_HIGHDPI);
    //naredba koja kreira prozor, prvi parametar je naslov prozora,
    //sledeca dva parametra odredjuju pocetnu poziciju prozora na ekranu(undefined znaci da nije odredjeno)
    //naredna dva su duzina i sirina prozora
    //poslednji parametar je flag – omogucava da na displejevima sa velikom gustinom bude okej

    windowSurface = SDL_GetWindowSurface(window);
    //ovim je omoguceno ocitavanje povrsine/pozadine

    if (NULL == window) //ovom naredbom se vrsi provera da li je prozor uspesno kreiran
    {
    std::cout << "Could not create window: " << SDL_GetError() << std::endl;
    return EXIT_FAILURE; //zatvara program ukoliko je uslov if-a tacan

    }

    if (!(IMG_Init(IMG_INIT_PNG)&IMG_INIT_PNG)) //proverava da li je proces inicijalizacije SDL_image-a uspeo
    {
    std::cout << "Failed to initialize SDL_image for PNG files: " << IMG_GetError() << std::endl;

    }

    SDL_Event windowEvent; //ova naredba upravlja dogadjajima koji se javljaju u programu

    imageSurface = IMG_Load("FNC.png"); //ucitava/aplouduje teksturu u program

    if (NULL == imageSurface) //vrsi proveru ucitavanja slike u program/prozor
    {
    std::cout << "SDL could not load image! SDL Error: " << SDL_GetError() << std::endl;
    }
    while (true) //true znaci da ce petlja while nastaviti dok sami ne stavimo uslov za izlazak
    {
    if (SDL_PollEvent(&windowEvent)) //ako se neki dogadjaj odvija na prozoru, proverice se sta se desava
    {
    if (SDL_QUIT == windowEvent.type) //ako je vrsta dogadjaja koja se desila "quit" dogadjaj onda ce se zavrsiti while petlja(a samim tim i aplikacija)
    {
    break;
    }
    }
    SDL_BlitSurface(imageSurface, NULL, windowSurface, NULL);
    //iscrtava sliku, prvi parametar je ono na sta se iscrtava slika
    //treci parametar specificira se povrsina na koju se iscrtava
    //drugi i cetvrti parametar nose NULL vrednost za sad**
    SDL_UpdateWindowSurface(window);
    //azurira se povrsina prozora(window)
    }
    SDL_FreeSurface(imageSurface);
    //pokrece se "ciscenje" povrsine sa prozora(inicijalizuje)
    SDL_FreeSurface(windowSurface);

    imageSurface = NULL;
    //postavlja se da povrsina prozora bude nepostojana/ociscenja
    windowSurface = NULL;

    SDL_DestroyWindow(window); //unistava prozor
    SDL_Quit(); //oznacava izlazak iz programa
    return EXIT_SUCCESS; //vraca vrednost da li se program uspesno zatvorio
    }

    Some insight would be helpful as I need to do this in a month(making a simple platformer game)…

  • Guillaume Globensky says:

    Hey, it's been a while since I took time to continue your tutorial but I had an assignment in class where I had to work with SDL and classes. I took on the half finished tutorial I had made and it just works so well. I was able to add Physics components with physics and lateupdate I used the GameObject to manage the entity pointer and manager reference, I added an int for the layer in the map file… when I'll have more time I'll redo the whole tutorial from the beggining. I wondered though… my teachers insists on us using cpp files for logic and I noticed your components don't seem to use them and I was wondering if there was a reason why or if it was just a stylistic question.

  • Goran Benak says:

    I am interested in the order of cleaning. Should we first destroy Renderer and then Windows instead of vice versa? After all, renderer has windows pointer…

  • Douglas Goddard says:

    Fucking legend.

  • Drew McDonald says:

    For those of you who want to use your favorite text editer with the terminal/command line instead of an IDE like VS or Xcode, check out this medium article: https://medium.com/@edkins.sarah/set-up-sdl2-on-your-mac-without-xcode-6b0c33b723f7

    It describes using homebrew for mac but you could use this link to find your preferred method on windows: http://lazyfoo.net/tutorials/SDL/01_hello_SDL/windows/index.php

  • Time to Tango! Harmony says:

    Hello,
    Thx for this great tutorial. I was wondering why do you use an init() function and a clean fonction, since we can use constructors and destructors instead of it !

  • Ramazan Eren says:

    Im having trouble with main.cpp's game->update game->handleEvents and game->render sections
    visual studio gives c3867 error… can anyone help me?

  • Sergio Montesinos Apaza says:

    Me acabo de comer una propaganda de 40 segundos, me pregunto quien es el culpable??

  • aurora maybee says:

    What is the difference between using #pragma once vs using #ifndef, #define, #endif?

  • Ali frikha says:

    666 likes
    holy ….

  • stathis stathopoulos says:

    You need Uint32 for the flags instead of INT

  • Hey Man says:

    Can you say me where you learned c++ programming? What is the main things that need to know in creating games with c++?

  • Oppsy Jaffa says:

    I'm going to watch this everyday until I remember everything, then i'll practice and try on my first 2d rpg 🙂

  • Draktharr says:

    Thank you for this tutorial.

  • TOFBRO YT says:

    there is a problem with init it says no member named 'init' in Game

  • Slayer_Rs says:

    how do I get that version of c++ like to select the templates ?

  • dustBoxed.isUpNORTH says:

    I don't know why but I constantly keep getting a fucking "lvalue required as left operand of assignment" error in every place there's anything referencing the isRunning or running booleans

  • Joao Araujo says:

    Hello sire. Shouldn't the dealocation be done in the destructor? Making the clean () function unnecessary?

  • Gabriel Makowsky says:

    Im new to coding and I keep getting this error: Value of type 'void' is not contexually convertible to 'bool' if someone could please help that would be great.

  • 苏陈标 says:

    you should call SDL_DestroyRederer before SDL_DestroyWindow on win7,otherwise sdl will throw exception.And thanks your video.

  • H123 says:

    If people are using visual studio and are still getting errors after trying all the different solutions, try putting #undef main under #include "Game.h" in the main file

  • TheAfricanPrince says:

    I'm getting Xcode and I'm gonna follow loads of tutorials

  • Burak Nazlim says:

    If fullscreen doesn't work properly use this: SDL_WINDOW_FULLSCREEN_DESKTOP

  • Ernesto Severinsen says:

    For those getting an SDL_main undefined error, you need to make sure your main function has the argument count and argument vectors as parameters: int main(int argc, char** argv).

    Also, make sure you have the SDL2.dll in the same output directory as your .exe or it won't link properly.

  • EstelyenTV says:

    Can't get this to build… I'm at 21:40, trying to get the window to show, but I get these messages:
    main.cpp(4): error C2143: syntax error: missing ';' before '*'
    main.cpp(4): error C4430: missing type specifier – int assumed. Note: C++ does not support default-int
    main.cpp(8): error C2061: syntax error: identifier 'Game'
    main.cpp(10): error C2065: 'SDL_WINDOWPOS_CENTERED': undeclared identifier

    Other than the first line which says #include "Game.h" because I'm on windows, my code is exactly the same as in the video (or at least I don't see any discrepancies). Is there something else I need to do to make this work on windows? Using Visual Studio 2017 Community

  • Vishnu Prasanth says:

    What is the limitation of doing it this way?

    #include "Game.h"

    //Game *game = nullptr;

    int main(int argc, char *argv[])
    {
    Game game;
    game.init("GameWindow", 800, 600, false);

    while (game.running())
    {
    game.handleEvents();
    game.update();
    game.render();
    }

    game.clean();
    return 0;
    }

  • Troy Durica says:

    I can't use SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
    I keep getting this output in XCode:
    Subsystems Initialized!…
    Window Created!…
    Renderer Created!…
    2018-10-21 21:57:03.996549-0400 Game[3437:483097] MessageTracer: load_domain_whitelist_search_tree:73: Search tree file's format version number (0) is not supported
    2018-10-21 21:57:03.996603-0400 Game[3437:483097] MessageTracer: Falling back to default whitelist
    Game Cleaned
    Program ended with exit code: 0

  • Brian Bachinger says:

    Just to note: 'new' should generally always be followed by a 'delete' when the object is no longer used. So you need to 'delete' the game pointer as it points to a dynamically created object in memory.

  • Shinpi Tekina says:

    the whole header thing is a bit confusing with visual studio

  • Zach G says:

    For anyone on a Mac having trouble getting the window to show up, I found a post on SO that fixed the problem. Add this loop to your code instead of the SDL_Delay, and it will show up:
    // 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);
    }

  • Andrew Lodge says:

    did anyone get this Object reference not set to an instance of an object.

  • Tim Papich says:

    Is there a shortcut for Visual Studio that can Autofill SDL functions with default arguments?

  • janči Zuletek says:

    dont get why with pointers, could make an instance and use .calls

  • Niclas Larsson says:

    You sir, have a memory leak 🙂

  • Aristos Athens says:

    Nice video, thanks for making it.

  • Shlacksy says:

    is there a video actually explaining aspects of SDL? im unsure of what flags are etc

  • Berkay says:

    am i the only one who gets no such file or directory error and can not compile code :/

  • João César de Lima says:

    Finally a C++ tutorial that I understand something! Thanks!

  • Parabalani says:

    I made my Game class a singletone. It's a good idea, isn't it? We don't want more than 1

  • Angry Sidra says:

    Hey, can I just use vim and include the sdl2 header file instead of using that IDE?

  • markus wil says:

    I learn so much new things from you , you are a very good instructor thank you

  • JRSoft says:

    Is it ok if I put a ternary operator for the fullscreen like so: (fullscreen) ? SDL_WINDOW_FULLSCREEN : SDL_WINDOW_RESIZABLE

  • Bright says:

    excuse me,how to compile and debug with vs code?My develop env is win10h64+vscode+mingw,thanks

  • Marshmello !!!! says:

    I am new to c++ can any one explain me why dont we write the functions in the class ?

  • BGivo says:

    Can someone please enlighten me as to why "init" stuff is not done in the constructor, and "clean" stuff is not done in the destructor?

  • Cinnabun says:

    I am using Code Blocks on Windows. I have the code just as you have it when you built, but I am getting errors:

    ||=== Build: Debug in 2D_Game_Engine (compiler: GNU GCC Compiler) ===|

    objDebugmain.o||In function `Z8SDL_mainiPPKc':|

    main.cpp|8|undefined reference to `Game::Game()'|

    main.cpp|10|undefined reference to `Game::init(char const*, int, int, int, int, bool)'|

    main.cpp|14|undefined reference to `Game::handleEvents()'|

    main.cpp|15|undefined reference to `Game::update()'|

    main.cpp|16|undefined reference to `Game::render()'|

    main.cpp|19|undefined reference to `Game::clean()'|

    ……SDL2-2.0.9liblibSDL2main.a(SDL_windows_main.o)||In function `main_getcmdline':|

    UsersvalvereleaseSDLSDL2-2.0.9-sourcefoo-x86..srcmainwindowsSDL_windows_main.c|175|undefined reference to `SDL_main'|

    ||error: ld returned 1 exit status|

    ||=== Build failed: 8 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

    Does anyone know what may be wrong?

  • AV3S Tube says:

    Very nice tutorial, although a bit old and I have to say I had to resolve a few bugs here and there(no big deal, they were annoying tho), the very good thing about it is the fact that you organize everything so neatly… In other tutorials the code is just all in function main, wihtout any classes or managing code and it's all left to the viewer, along with all the issues that may come with organizing it all 😛
    Overall, thanks for this tutorial! 😀

  • Andrei Calazans says:

    Thanks for this tutorial mate!!

  • Simon Myers says:

    I'm trying to get my code to work, but when I run it at the end of this lesson, I get an error window named '2D_GameEngine.exe' saying "The application was unable to start correctly (0xc000007b). Click OK to close the application." When I try to use breakpoints on the main function, it still spits out the error. Can someone help me?

  • Jerome Marfleet says:

    Can't get it to run. I get `INFO: Unable to initilize SDL: No available video device
    Game Cleaned. ` I'm on Ubuntu 18.04. tried installing a bunch of i386 packages, but no luck.

  • Paiz says:

    I've initialized The game on the Main.cpp file but while i am running the program it says that the game was cleaned.

  • samljer says:

    you also need delete Game; after Game->clean();

  • Jerome Marfleet says:

    My Game Immediately Goes
    `Game Cleaned`
    SDL_GetError() states
    `No available video device`

  • xView says:

    i got unresolved external symbol

  • Tedy400 | Dennis says:

    <stido.h> not <studio.h>, i can't read

  • pokepoke guy says:

    Beautiful video! Liked and subscribed !
    That pure joy in your face at 21:50 is the fun in programming that draws all of us to it!

  • logan graham says:

    why not just …

    int main(){
    Game game("Game",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,800,600,false);
    while(game.running()){
    game.HandleEvents();
    game.Update();
    game.Render();
    }
    }

    and instead of having separate functions init and clean just do that in the Constructor and Destructor

  • Colorics says:

    im too stupid for C++

Leave a Reply

Your email address will not be published. Required fields are marked *