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

Поділитися
Вставка
  • Опубліковано 23 вер 2024

КОМЕНТАРІ • 476

  • @MackMillicco
    @MackMillicco 4 роки тому +198

    For Visual Studio Programmers, the biggest (and smallest) issue I had was giving me LNK issues.
    The solution for me was in main.cpp. Remove the "const" type from "const char * argv[]" in the main function. It took way to long to figure this out, hope it helps someone.

  • @krakenbyte7377
    @krakenbyte7377 3 роки тому +35

    You cannot imagine the satisfaction when the window started and everything was going correctly. Much Love

  • @ripedpaper
    @ripedpaper 7 років тому +49

    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!

    • @CarlBirch
      @CarlBirch  7 років тому +8

      Alec Dosier No problem :)

  • @NoodleFlame
    @NoodleFlame 3 роки тому +18

    There are some issues with this code...
    1) If SDL fails to create a window you don't want to then attempt to create a renderer.
    2) Likewise if the renderer fails to initialize you don't want to set isRunning and start the update loop.
    3) Because initialization can fail you should really be returning success/failure from this routine so that it can be handled appropriately.
    Also a speed tip for those watching, after you have defined your class, just copy paste that into the cpp file. You don't need to type it all out again. You can replace the semi colons with open close braces and you are good to go, it will save you a tonne of time.

  • @joaocesardelima4318
    @joaocesardelima4318 5 років тому +19

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

  • @ole-martinbroz8590
    @ole-martinbroz8590 4 роки тому +122

    windows: in game header file add #define SDL_MAIN_HANDLED before including sdl.h

    • @Paulo28632
      @Paulo28632 4 роки тому

      u r the man

    • @mermin5239
      @mermin5239 4 роки тому

      omg i love u

    • @emanuellcarvalho5419
      @emanuellcarvalho5419 4 роки тому

      you saved me, thank you so much

    • @3freezeen
      @3freezeen 4 роки тому

      Wow thanks it worked!

    • @INeedAttentionEXE
      @INeedAttentionEXE 4 роки тому +2

      Pain in the arse, what program doesn't have an int main(), why did they even think of this as a weird edge case.
      Literally the first google search for "unresolved external symbol main", gives you a stack overflow article, and the second answer tells you about SDL_MAIN_HANDLED

  • @n1pster3
    @n1pster3 3 роки тому +2

    I love these tutorials. It's slow enough that I can follow along but not slow enough to be boring or repetitive. Thank you.

  • @averageanon007
    @averageanon007 2 роки тому +21

    For those compiling via g++ or make, the compilation command is "g++ -I src/include -L src/lib -o main.exe *.cpp -lmingw32 -lSDL2main -lSDL2"

    • @cgl4de
      @cgl4de Рік тому

      epic gamer

    • @orphixigl1476
      @orphixigl1476 Рік тому +2

      If you also write -Wl,-subsystem,windows you can hide the console

    • @homarslamson
      @homarslamson Рік тому +2

      im imagining someone finding this comment after trying to find the answer for hours and it working must be a wild feeling

  • @remotebeef4247
    @remotebeef4247 6 років тому +90

    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!

    • @ArkThaCreator
      @ArkThaCreator 6 років тому +32

      thats psuedo codeing fran :D

    • @SoftBreadSoft
      @SoftBreadSoft 5 років тому +10

      It can be a good idea with complex projects to work out the structure or flow of how the program/game will work first.

    • @boblee5556
      @boblee5556 3 роки тому +1

      @@ArkThaCreator stuff like this>>How you can tell people never took a proper class/ had anyone around them who writes code

    • @Kokurorokuko
      @Kokurorokuko 3 роки тому +1

      @@boblee5556 Perhaps the person didn't take informatics classes or was simply not interested at that time in this topic. No need to be that harsh.

    • @boblee5556
      @boblee5556 3 роки тому

      ​@@Kokurorokuko I really wasnt trying to be that harsh its just like we learned that in the mandatory CIS 101 for all students in freshmen year at my Junior college... its just hella easy to tell when people have never had any formal instruction... (protip, move to a state with socialized college, get wage-slave job to pay bills, wait one year to qualify as resident... profit)

  • @Carl-xp2ur
    @Carl-xp2ur 4 роки тому +2

    Hi, just a question:
    Why did you define the game variable in the global scope and not inside the main function itself?
    Also why did you create it on the heap using new? And should it not because of that be explicitly removed using 'delete game' after the cleanup?
    thanks in advance!

  • @theotherquou
    @theotherquou 4 роки тому +168

    you know hes a real programmer when he start writing pseudo code straight into the ide

    • @dudefrombelgium
      @dudefrombelgium 3 роки тому +30

      We must explain to the machine nicely before we decide to force it to do anything, just in case.

    • @eien7228
      @eien7228 3 роки тому +1

      not rly actualy, tht just means he has created the code b4

    • @Shootingstarsfizzy
      @Shootingstarsfizzy 2 роки тому +1

      real programmer would create a gui for a flowchart

    • @DeveloperRich
      @DeveloperRich 4 місяці тому +2

      @@Shootingstarsfizzy ui programmers pretending to be real programmers

  • @Khaliljt
    @Khaliljt Рік тому +3

    Quick question. Why do you not just declare and implement the game class in the same file?

  • @TonyDaExpert
    @TonyDaExpert 5 років тому +7

    Nice! After learning some C++ essentials I was wondering what I would look for next, looks like this will do

  • @Chris-cs7nv
    @Chris-cs7nv Рік тому +2

    All this at 21:40 does is create linker errors: LNK2019 unresolved external symbol SDL_SetRenderDrawColor referenced in function "public: void __cdecl Game::init(char const *,int,int,int,int,bool)" (?init@Game@@QEAAXPEBDHHHH_N@Z) is the first one...
    Edit: make sure to watch the first video again in that case, I didn't add in linker - > input the dependencies... Also, there seems to be a naming conflict with the main function already being defined so somewhere so changing it to char* argv[] instead of const char* argv[] seems to fix that or you can #undef main in main.cpp... but I don't really understand so maybe it won't work for you :'(

  • @Nero-dq4ep
    @Nero-dq4ep 4 роки тому +1

    btw, these codes work just as well on visual studio, right? or must something else be done in visual studio to make functional a game?

  • @szilardmolnar4232
    @szilardmolnar4232 4 роки тому +6

    Thank you for the tutorial! The pace makes it easy to follow and understand.

  • @2kOlay
    @2kOlay Рік тому +1

    This is awesome. I've been looking for a series like this that shows you how to build a game in Cpp without relying on a prebuilt engine. Good stuff!

  • @foxzyx
    @foxzyx 11 місяців тому

    19:05 what are you doing here? what is "Game *game"? because when I compile it gives me an error saying "game" was not defined in this scope even though I followed the tutorial to a T.

  • @marcus-school
    @marcus-school 3 роки тому +1

    Thank you so much for the tutorial! After 3 hours of unsuccessful searches, I finally found what I was looking for! You are the best!

  • @quintinlake9513
    @quintinlake9513 3 роки тому

    I do not know, and i hope anyone out their is needing this at this moment. Here it is, download SDL with Brew, I have no CLUE what it did but i downloaded all the libraries needed and it is working now. thank you for my ted talk

  • @xStefaKO
    @xStefaKO 6 років тому +1

    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
    #include
    #include
    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

    • @andrewlodge241
      @andrewlodge241 5 років тому

      to create a cpp as well as a hpp file at the same time create it as a class

  • @dasmaffin1633
    @dasmaffin1633 3 роки тому

    Hey Ive only been programming in c# so far and you are using all that stuff in the game class like its static is that just by default in cpp or is there something behind that?

  • @tomiivaswort6921
    @tomiivaswort6921 3 роки тому +1

    Glad to see that other programmers write flase instead of false.

  • @DavePaddy
    @DavePaddy 3 місяці тому

    Thank you, this is great, I will definitely continue with the series. And I like how clean the main file is.

  • @matthewexline6589
    @matthewexline6589 9 місяців тому +1

    Thank you so much for your work. These videos are marvelous. If you don't mind a question though, I was following along with a different tutorial as well and they used "if( SDL_Init( SDL_INIT_VIDEO ) < 0 )" instead of "if (SDL_Init(SDL_INIT_EVERYTHING) == 0) {" and I was wondering if you have an opinion on this or not. For games is it simply just best to init everything all at once?

  • @Commonfiend
    @Commonfiend 10 місяців тому

    Thanks. This was a good start for me. I'm taking a data structures class next semester and so I decided to try to make a simple playing card game I made up as a way to brush up on my c++ and hopefully I will have a portfolio piece to add to my resume.
    I'm using Eclipse in Kubuntu 22.04 and I was getting all the dreaded SDL undefined reference errors. I tried adding it to the linker and every other suggestion and finally deleted and purged both Eclipse and SDL2 and reinstalled both using the C++ version download from the Eclipse website. SDL I downloaded using apt and this series libsdl2-dev libsdl1.2-dev libsdl-image1.2-dev libsdl-mixer1.2-dev libsdl-ttf2.0-dev. Then I started a new Eclipse C++ and added SDL2 to the linker and path to the compiler includes. Now everything is working.

  • @voxeledphoton
    @voxeledphoton 5 років тому +14

    oof had my volume up all the way with earbuds in and.. that start sound... x....x
    thanks for the tut ;D

  • @zachgillis2552
    @zachgillis2552 5 років тому +3

    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);
    }

  • @yoonjiwoo7487
    @yoonjiwoo7487 5 років тому +1

    finally a video where using namespace std isnt involved. thank you for sharing this, its been helpful

    • @MrEvilyogurt
      @MrEvilyogurt 4 роки тому

      I’m new and curious why don’t u want to just declare it at the top so u don’t need to use std:: in everything ?

    • @thedeagle1007
      @thedeagle1007 3 роки тому +1

      @@MrEvilyogurt "When C++ was originally designed, all of the identifiers in the C++ standard library (including std::cin and std::cout) were available to be used without the std:: prefix (they were part of the global namespace). However, this meant that any identifier in the standard library could potentially conflict with any name you picked for your own identifiers (also defined in the global namespace). Code that was working might suddenly have a naming conflict when you #included a new file from the standard library. Or worse, programs that would compile under one version of C++ might not compile under a future version of C++, as new identifiers introduced into the standard library could have a naming conflict with already written code. So C++ moved all of the functionality in the standard library into a namespace named “std” (short for standard)."
      This basically means by putting using namespace std; at the top of your program subjects you to all the above issues stated with having standard library in your global namespace.

    • @MrEvilyogurt
      @MrEvilyogurt 3 роки тому

      @@thedeagle1007 interesting thank you !

    • @thedeagle1007
      @thedeagle1007 3 роки тому

      @@MrEvilyogurt No problem! I got that excerpt from the website I use to learn C++.
      Learncpp.com.
      It's free and has been quite helpful.

  • @BenUsher-m9v
    @BenUsher-m9v 11 місяців тому

    Thanks so much for this series! Love your pacing and information level ^^ hoping I can keep up through the whole tutorial. Just started with newest VC and SDL2 builds, everything working great so far. Cheers!

  • @mccordinator
    @mccordinator 4 роки тому +1

    If your app crashes before it ever shows the window:
    I was getting a crash before my window would load with an error like "library not loaded no suitable image found" for SDL2. And something else about a matching team ID
    I fixed it by:
    1. going to Project Settings > Build Settings > Other Liner Flags > Add "-framework SDL2"
    2. going to Project Settings > Signing & Capabilities > Hardened Runtime > Check the box next to "Disable Library Validation"
    3. in Project Settings > Signing & Capabilities > Signing > "Automatically manage signing" is checked, I have my personal account chosen for "Team", I have "Signing Certificate" set to "Sign to Run Locally"
    Hopefully, after you set all these as described above, Clean, Build, and Run it will work.

  • @ZiggyPebble
    @ZiggyPebble 7 років тому +1

    Really enjoyed your first tutorial. I look forward to more to come.

    • @CarlBirch
      @CarlBirch  7 років тому

      Joel Quanbeck thank you :) next episode will tomorrow 21st

  • @elifuller8671
    @elifuller8671 Рік тому +1

    trying to follow along on VS. Having some frustrating bugs
    Game.cpp(8,95): error C2761: 'void Game::init(const char *,int,int,int,int,bool)': redeclaration of member is not allowed
    Game.cpp(9,1): error C2447: '{': missing function header (old-style formal list?)
    I cant get it to run and cant figure out why, anyone know what's going on??
    the second one is particularly frustrating bc the code mirrors the video

  • @andreykolechko5911
    @andreykolechko5911 4 роки тому +4

    if you are getting unresolved external something on windows, remove "const" from main, eg. int main(int argc, char* argv[]), it helped me

    • @denizhandeniz5207
      @denizhandeniz5207 3 роки тому

      Thank you :), how did you find this fix?

    • @danieltietz4146
      @danieltietz4146 3 роки тому +1

      @@denizhandeniz5207 I'm find this fix here: stackoverflow.com/questions/31761045/sdl2-issue-undefined-reference-sdl-main

    • @denizhandeniz5207
      @denizhandeniz5207 3 роки тому +1

      @@danieltietz4146 thnk u

  • @dasp125
    @dasp125 4 роки тому

    For your error handling in the Init method couldn’t you use SDL_Log() and SDL_GetError()
    eg. SDL_Log(“Unable to initialise SDL: %s”, SDL_GetError());
    Instead of having to include iostream, or doesn’t it really effect performance that much?

  • @gglobensky
    @gglobensky 6 років тому +11

    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.

  • @SuperFluffyCarrot
    @SuperFluffyCarrot 6 років тому +3

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

    • @younlok1081
      @younlok1081 5 років тому +1

      so did u make it ?,

    • @h-0058
      @h-0058 4 роки тому

      @@younlok1081 Doesn't seem like it

    • @younlok1081
      @younlok1081 4 роки тому

      @@h-0058 lol maybe

    • @atlantic_love
      @atlantic_love 4 роки тому +2

      No. The next day he probably said he was going to look at a Da Vinci painting every single day until he memorized the strokes, and then create his first rectangle house.

    • @s7v7n97
      @s7v7n97 3 роки тому

      Hey its been 3 years did you make it

  • @dire284
    @dire284 4 роки тому +1

    Hello, im getting a strange error when i try to compile. Variable 'Game::isRunning' is uninitialized aswell as renderer and window. i have been trying to find solution but cannot find anything.

  • @FPChris
    @FPChris 3 роки тому

    Great tutorials. What happens if you call init() more than once? Shouldn't you check the window and renderer exist and destroy them before creating new ones? or would SDL handle that. Thanks.

  • @manorujas9352
    @manorujas9352 6 років тому

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

    • @farooqar
      @farooqar 6 років тому

      Yes, we can do that, but we will have to delete the game object explicitly at the end of main(). Game's Destructor function doesnt get called automatically when the program terminates.

  • @GiriPrahastaPutra
    @GiriPrahastaPutra 7 років тому +2

    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!

    • @CarlBirch
      @CarlBirch  7 років тому

      Giri Prahasta Putra Thanks :)

  • @sugarsay
    @sugarsay Рік тому

    I'm using codeblocks and get the following errors:
    Game.cpp||In function 'void init(const char*, int, int, int, int, bool)':|error: 'window' was not declared in this scope|
    Game.cpp|19|error: 'renderer' was not declared in this scope|
    Game.cpp|24|error: 'isRunning' was not declared in this scope|
    Game.cpp|27|error: 'isRunning' was not declared in this scope|
    Game.cpp||In member function 'void Game::clean()':|
    Game.cpp|53|error: 'SDL_QUIT' cannot be used as a function|
    Any help would be appericiated

  • @poupdujour
    @poupdujour 4 роки тому +1

    Fantastic tutorial. Easy to follow and straight to the point. Thanks!

  • @MultiTrickster121
    @MultiTrickster121 6 років тому +9

    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

    • @magyarfiu
      @magyarfiu 6 років тому

      Very very THX

    • @nexonexo484
      @nexonexo484 5 років тому

      thanks but can you tell me what type of project I need to do? (in visual studio)

    • @joshuatravis9070
      @joshuatravis9070 5 років тому

      you are a freaking genius!!! spent an hour trying to figure that out!!

    • @Rheenabyte
      @Rheenabyte 5 років тому

      @Der Absender I believe it's because the "SDL.h" header includes a header called "SDL_main.h" which has a declaration for the main function with the signature int main(int argc, char *argv[]). If you write a plain main definition as int main(), that puts a 2nd valid main function definition in the global scope. #undef main before YOUR main will remove the definition SDL has put in place and remove the ambiguity. Another way to solve it is to just declare main with the parameters (int argc, char *argv[]) to match the declaration SDL has. Hope this helps a week later.

  • @averageanon007
    @averageanon007 2 роки тому +2

    in the Game.hpp file, you should set `bool isRunning=false` instead of just `bool isRunning` in order to avoid an infinite loop when Game is not initialized

  • @auroramaybee6580
    @auroramaybee6580 6 років тому +4

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

  • @NeilRoy
    @NeilRoy 7 років тому +9

    You don't need an init() function, you should be using the Game classes constructor, it is run automatically as soon as you create the Game object without any need to call init(). If you don't use a constructor, you may as well use C (which I actually prefer to be honest).

    • @CarlBirch
      @CarlBirch  7 років тому +1

      Neil Roy Hi Neil, it’s true that the game class could be initialised in the constructor, but I was in the mindset that doing it in an init function would allow me to reinitialise the game when I need it to be, not sure yet if I will be implementing restarts like this yet. I’ll be doing some sort of world/game manager at some point soon, so I’ll decide then. So yes it may end up in the constructor. For the use of c++ I will be using function templates and smart pointers later on in the series

    • @Baekstrom
      @Baekstrom 7 років тому +1

      Then call the function reset(). Requiring the class to be initialized after construction in order to make it operable is a violation of the RAII principle.

    • @Baekstrom
      @Baekstrom 7 років тому

      You throw an exception.

    • @Skandalos
      @Skandalos 5 років тому

      Would the same be true for clean() code actually belonging into the destructor?

    • @mathiasman
      @mathiasman 5 років тому

      @@Skandalos Yes that would be best practice. Same goes for the allocated game object, much better to just create a local game object, so we have better control over when it is destroyed.

  • @Fukiyel
    @Fukiyel 7 років тому +1

    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 ^^

    • @CarlBirch
      @CarlBirch  7 років тому

      +Fukiyel it should be quicker than that? Are there any logs you have access to?

    • @Fukiyel
      @Fukiyel 7 років тому

      Well i "corriged it" by removing the "if SDL_Init(SDL_INIT_EVERYTHING) == 0" check, i don't know why it was so slow to check for initialisation for me ._.
      But well, it's normal now that it's removed, thanks anyway x')

  • @oskardeeream1846
    @oskardeeream1846 2 роки тому

    why did you choose to allocate your game obj on the heap. Also you spent the time to free the SDL stuff in the destructor but then didn't even free the game obj itself?

  • @estelyen
    @estelyen 6 років тому

    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

    • @estelyen
      @estelyen 6 років тому

      Figured it out. For everyone running into this problem: I started this solution as a console application and Visual Studio needs me to #include "pch.h" in this situation. Took me all weekend to realize that I have to #include "pch.h" BEFORE #include "Game.h".

  • @aogposton
    @aogposton 10 місяців тому

    Such valuable info. So grateful. Thank you for the time you put into this tutorial.

  • @Flitzpieps123
    @Flitzpieps123 8 місяців тому +1

    The Error list says, that the "Game::isRunning", "Game::renderer" and the "Game::window" variables aren't initialised. "A Membervariable must always be initialised (type 6)."

  • @calvinmillett
    @calvinmillett 7 років тому +1

    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!

    • @b.l.8755
      @b.l.8755 2 роки тому

      I am also having this error and am interested in any feedback

  • @roanewilliams5868
    @roanewilliams5868 6 років тому +2

    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.

  • @av3stube480
    @av3stube480 5 років тому +3

    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 :P
    Overall, thanks for this tutorial! :D

  • @Solo6R
    @Solo6R 7 років тому +5

    Really enjoying these videos. Will you eventually be capping the FPS or teaching us a time step method? Introduction of delta time?

    • @CarlBirch
      @CarlBirch  7 років тому +3

      GunnDawg There is a video on frame rate, and we will be using a ‘deltaTime’ system in the future. Just getting things done the simplest way then will be making things more robust as we progress with the engine and game.

  • @silviogames
    @silviogames 7 років тому +2

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

  • @harrywright6876
    @harrywright6876 6 років тому

    Really enjoying this series so far, great stuff!

  • @markus72wi
    @markus72wi 5 років тому +1

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

  • @antoniocs8873
    @antoniocs8873 6 років тому +1

    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?

    • @tommasobonvicini7114
      @tommasobonvicini7114 6 років тому

      Antonio Cs
      I thought the same thing, not really as far as I can tell.

  • @roguej47
    @roguej47 5 років тому +1

    Doing this on visual studio. I've worked a lot with c#, so I'm thinking I can jump right into this! I was wrong... for anyone facing a wall of 200 errors right off the bat, try messing with your includes until that error number reaches zero. Good luck!

    • @dudefrombelgium
      @dudefrombelgium 3 роки тому +1

      how did you get this to work in visual studio. i am very new to coding so im looking for all info about it i can get.

  • @steampoweredmaniac5359
    @steampoweredmaniac5359 4 роки тому +1

    I watched the previous video on how to set up SDL on windows, and coming from that tutorial to this one, I have no idea what I’m supposed to do. I have to create a completely different project? What about all that set up I just did?

    • @xanderpizza2759
      @xanderpizza2759 4 роки тому +1

      i think hat was just to lean how to put in sdl not specifically for this

  • @ZeroSleap
    @ZeroSleap 2 роки тому +2

    The little Running() function in game class could be more aptly named GetIsRunning cause its essentially a getter.
    Also the init function has the problem of still saying its running even if window and renderer return false,plus theres no debug message if those are false also But im nitpicking.Nice tutorial anyway.

  • @bandannaman510
    @bandannaman510 5 років тому +2

    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?

    • @Nodal488
      @Nodal488 5 років тому

      Getting the same issue !

    • @bandannaman510
      @bandannaman510 5 років тому

      @@Nodal488 I was able to move past this problem, but I forgot what I did to fix the error. Oh I remember, I googled the error and found this page www.drivereasy.com/knowledge/how-to-fix-the-application-is-unable-to-start-correctly-0xc00007b-error/
      I think I reinstalled the SDL2 package and it worked. I was using an external hard drive and had to move the downloaded files to get everything where it needed to be. After I reinstalled it was smooth sailing. Give that a try and let me know what you find!

    • @Nodal488
      @Nodal488 5 років тому

      Fixed ! Thanks !

  • @TuanAnhNguyen-ox3lh
    @TuanAnhNguyen-ox3lh Рік тому

    Why in Game::handleEvents I always have error :
    invalid use of member 'int Game::isRunning()' (did you forget the '&' ?)
    How can I fix it ?

  • @mbrenneman0411
    @mbrenneman0411 7 років тому +3

    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'

  • @ridilatrois7991
    @ridilatrois7991 5 років тому +1

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

    • @RobotronSage
      @RobotronSage 5 років тому

      that would be my best guess yeah

    • @RobotronSage
      @RobotronSage 5 років тому

      i mean the flags variable is set for your flags which you can assign to functions
      flags is initialized a value of 0
      so instead of ''if (fullscreen)'' you could put other conditions like if (fullscreen == 1) {flags = SDL_WINDOW_FULLSCREEN;}
      and have another condition such as (fullscreen == 2) { flags = SDL_WINDOW_RESIZABLE;}
      You could rename ''fullscreen'' variable to ''screensize'' or something.
      It would take some reworking the code but nothing too difficult

  • @solidstatejake
    @solidstatejake 6 років тому +2

    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.

    • @seanld444
      @seanld444 6 років тому

      It's an old standard for developers that worked before things like Git were in widespread use. I personally just put my implementation code in the header file. Much less file clutter. It's also a lot easier to edit than jumping back and forth through files just to change the name of a class's private data member's name.

    • @justaar4036
      @justaar4036 6 років тому

      try renaming the .hpp to .h

  • @piyushsingh1985
    @piyushsingh1985 3 роки тому

    In visual studio 2019 Press Ctrl+. to trigger the Quick Actions and Refactorings menu and select Create Declaration / Definition from the context menu (quicker than typing)

  • @401281
    @401281 4 роки тому +1

    I'm having trouble understanding why "new" was used for the Game object. Why does Game need to be on the heap?

    • @yassine321
      @yassine321 4 роки тому +1

      cauz the instance is a pointer and we should allocate a space for im by using new

  • @2MSTennis
    @2MSTennis 3 роки тому +2

    Thank you for the wonderful tutorial!
    I ended up naming the Header with .h instead of .hpp.
    I ended up getting an error "LNK2019" unresolved external symbol: "public: bool_cdecl"...
    The pop-up error says, "Unable to run program...the system cannot find the file specified."
    Any and all help with be deeply appreciated!
    Thanks in advance,
    C

    • @gavinkujava6141
      @gavinkujava6141 2 роки тому +1

      I’m having a fairly similar issue, did you ever get it solved? I believe it’s a linker error but in my case it looking for a file that I don’t think exists

    • @onehello844
      @onehello844 2 роки тому

      @@gavinkujava6141 hey if your still having this issues delete remove "const" from main, eg. int main(int argc, char* argv[]) found it in the comments and it works

  • @苏陈标-c5k
    @苏陈标-c5k 6 років тому +6

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

    • @zyrxom
      @zyrxom 3 роки тому

      yeah I learned that the hard way i ended up crash my system after testing my game a few times and couldn't figure out why well it was because memory leaks

  • @PaizGamer
    @PaizGamer 5 років тому +4

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

  • @cloudboysmusic5223
    @cloudboysmusic5223 6 років тому +54

    int main(int argc, char *args[])
    if y'all having trouble.

    • @thisisgod2639
      @thisisgod2639 6 років тому +1

      omg I'm laughing here hahahahaha

    • @gglobensky
      @gglobensky 6 років тому +3

      It actually solved my LNK2019 problem and I don't get why. I'm pretty new to SDL and programming... Thanks a lot!

    • @thisisgod2639
      @thisisgod2639 6 років тому

      This allow SDL to be cross-platform

    • @amrojjeh
      @amrojjeh 6 років тому +6

      @This is God
      It doesn't allow it to be cross platform, that is just the definition of it's entry point. The function needs to have the same "signature" for the linker to be able to find it properly.

    • @collosiskdway
      @collosiskdway 6 років тому +1

      when i used "const char" it was attempting to search for another function which broke the program.

  • @jeromemarfleet9524
    @jeromemarfleet9524 5 років тому

    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.

  • @thomasgipson7120
    @thomasgipson7120 2 роки тому

    What's the point of the game class if it only gets one instance? Why not have pure functions handle the methods? (I haven't gone further than this video, so please excuse my ignorance of your future videos on this series)

    • @Gr-wv9rb
      @Gr-wv9rb 2 роки тому

      just to make it look nicer i guess, usually there is player class as well to make only one player. That's just how oop works

  • @facundopop
    @facundopop 4 роки тому +1

    Hi thanks for the turorial but I have 2 errors SDL_init is undefined and isRunning is undefined. I hope anyone can help me i am using visual studio 2019

  • @erto6144
    @erto6144 6 років тому +3

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

  • @ifstatementifstatement2704
    @ifstatementifstatement2704 7 років тому +66

    Is this a mac? why does everyone seem to be using mac to program games? i've always used windows.

    • @CarlBirch
      @CarlBirch  7 років тому +13

      +Seb B Hi Seb, it is a Mac. However because I'm using C++ and SDL2 the code is cross platform including Linux as well. All further episodes have/will been recorded in windows using visual studio 2017. Yes the Mac does seem to be on rise but windows still dominates the industry. Macs are a great machine fully capable and also can dual boot windows which makes it perfect for me.

    • @ifstatementifstatement2704
      @ifstatementifstatement2704 7 років тому +5

      Hey thanks for the reply! I have been programming on windows since 2004 when i got my first pc. Started programming in 1997. I've been programming a game using free pascal (using notepad++ and separately the free pascal compiler) and sdl 2 for about a year now. Yeah, i noticed that mac is used more and more. Songbringer and Axiom Verge were programmed on mac, i believe.

    • @CarlBirch
      @CarlBirch  7 років тому +1

      +Seb B No problems, Pascal?I have never really spent any time with with that. My first computer was an AST Win 95, can't remember it being as bad as its reputation. I came to programming around the same sort of time with Java and VB .Net. I Think I was around 17/18 when I moved to Linux and stayed there for a few years, it was then I started learning C++, then University for a game dev degree, which I went to windows then to a Mac and have never looked back. Best of both worlds :) Yes songbringer is done on Mac with Xcode using Cocos-2DX I believe.

    • @ifstatementifstatement2704
      @ifstatementifstatement2704 7 років тому +2

      Nice. I never studied programming. Been doing it daily as a hobby for 20 years. I taught myself everything. Most of what I learned was when the internet was not big like today, so did not have access to tutorials and updated compilers and IDEs. Don't know if that qualifies me for an actual programming job. I currently work in Accounts and the company relies on me to automate a lot of the tasks and finance processes for different teams (they are gaining a lot by paying me less than what it would cost them to outsource this). I use visual basic advanced and visual basic script in macros in excel. But I am also programming my own game (rpg about exploration and story, procedurally generated) in my free time, which I plan to sell one day.

    • @CarlBirch
      @CarlBirch  7 років тому +1

      Yeah I remember those days, We had Encarta on a Cd-Rom instead of Wikipedia, no google or UA-cam. It's more about experience than qualifications. How many projects you've shipped etc. Should ask for a raise ;). Yeah I love RPG's I was watching the songbringer devlogs which inspired me to make this series, hopefully it helps people. All you can do is keep at it, you will make it :)

  • @leonisadonis
    @leonisadonis 2 роки тому

    A great tutorial but I have to ask shouldn't we add a delete game; bellow game->render(); in the main.cpp because if we don't the pointer won't be deleted and we will get a memory leak!!?

    • @armincal9834
      @armincal9834 2 роки тому

      when program ends then all the memory allocated to the game will be cleaned automatically anyways, memory leak happens when you keep adding to the heap in a loop without freeing it, here we only do it once when we execute new() so to answer your question, no

  • @johnnypepperonii
    @johnnypepperonii 4 роки тому

    Hi, im running on the currently newest version of XCode but no matter how much i try, i just dont get the cascade to work, i have my game.cpp, game.hpp and my main.cpp, even with all the includes, nothing is getting recognized (only got this with XCode, on VS19 there is no problem with recognizing the header)

  • @fabiboiii
    @fabiboiii 7 років тому +1

    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)

    • @CarlBirch
      @CarlBirch  7 років тому

      Fabian Roland there will be a video in the future on memory management, as it stands now when the program exit the os reclaims the memory anyway. I understand this is not best practice, but the code base has and will change a lot and this issues will be fixed.

  • @hon420
    @hon420 Рік тому

    Thank you. You helped me a lot with my coursework! Very informative, this is exactly what I needed help with 👍 :D

  • @axelittle8463
    @axelittle8463 Рік тому

    lil tip for my windows friends select all in game.h, right click and click top option Create Definition

  • @Skulltroxx
    @Skulltroxx 3 роки тому +1

    in windows, the console only prints out "Game Cleaned" and exits out... (Press any key to continue)
    there is no window shown, anyone got a solution for that?

  • @notecalienta
    @notecalienta 6 років тому

    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?

    • @xhjb4ever
      @xhjb4ever 3 роки тому

      ever get this solved, well if you didnt and dont want to solve it im still going to tell you, change the solution platform to x64 instead of x86

  • @gotsinging6306
    @gotsinging6306 7 років тому +2

    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

    • @gotsinging6306
      @gotsinging6306 7 років тому

      Edit - Just solved it .. changed constant char to just char in every file 😊

  • @armincal9834
    @armincal9834 2 роки тому

    is there a list out there that contains all the absolute essential APIs? the complete list consists of thousands of functions(and methods) and i don't know where to look, I'm talking about methods like SDL_INIT_EVERYTHING and other basic APIs that one needs to create the skeleton of the game.

  • @Scotty-vs4lf
    @Scotty-vs4lf 6 років тому

    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

  • @checkmeout4298
    @checkmeout4298 6 років тому +6

    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

  • @michaelcastelpoggi1735
    @michaelcastelpoggi1735 2 роки тому +1

    Been stuck on an undefined member variable error for all the Game::functions() trying to get the window to render on Windows for the longest time. Code looks identical checking my one-liner g++ statement in case its the issue, which I used for a similar tutorial the other day... will post if I figure out my error :P Using VSCode
    What I'm currently working with:
    all:
    g++ -Isrc/include -Lsrc/lib -o main main.cpp -lmingw32 -lSDL2main -lSDL2

    • @michaelcastelpoggi1735
      @michaelcastelpoggi1735 2 роки тому +1

      I ended up referencing both the header and the cpp file in my main.cpp. I'm still not sure why this was necessary. The tutorial did not require importing both, and despite game.cpp not being referenced game.h he only needs to reference game.h in his main.cpp. I understand that he may have a more complete IDE than VSCode and not a g++ makefile statement, but to me my solution seems off/lacking an understanding of my compiler, as I did somethign similar to make a previous tutorial work. Thanks for the tutorial and if anyone has insight on my lack of understanding feel free to comment

  • @breenud39tv
    @breenud39tv 3 роки тому +2

    I can't get the first stuff down #ifndef Game_hpp and #define Game_hpp I'm using windows..

    • @breenud39tv
      @breenud39tv 3 роки тому +1

      Just needed an #endif/*Game_hpp*/ I'm good lol

  • @darthnegativehunter8659
    @darthnegativehunter8659 4 роки тому +1

    you need to add the include library and use a syntax like this #include

  • @goranbenak1201
    @goranbenak1201 6 років тому

    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...

  • @logangraham2956
    @logangraham2956 5 років тому +6

    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

    • @akira9449
      @akira9449 5 років тому +1

      it's probably late but, i think it's bc of reusable code i mean in some cases you wanna call constructor and initialize some variables then in specific condition it will call the function and boom there is a window. Some times reusable code matters

    • @logangraham2956
      @logangraham2956 5 років тому

      @@akira9449 ok , kind of makes sense ,
      you could also pass the class "object" by reference as well
      but i guess this method would be slightly easier.

    • @logangraham2956
      @logangraham2956 5 років тому

      just need to make sure you have it like
      void somenewfunction(classname objectname){
      //continue using the same "object" here
      }

  • @l75rd83
    @l75rd83 6 років тому +2

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

  • @morbidrsa
    @morbidrsa 3 роки тому

    In your Game::init() you're checking if SDL is properly initialised. Then you create a window and check that and a renderer. But you don't return Game::init() or create the renderer inside the if (window) block, so won't SDL_CreateRenderer() trip over a NULL pointer if SDL_CreateWindow() fails? Also if either SDL_CreateRenderer() or SDL_CreateWindow() fails you still set isRunning to true. This is why you should never do success handling but always do failure handling.

  • @Stan10785
    @Stan10785 7 років тому +1

    Great job I hope this continues!

    • @CarlBirch
      @CarlBirch  7 років тому

      +Stan10785 Thanks ;) It will!

  • @timpapich8500
    @timpapich8500 5 років тому

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

  • @Sinbad1999
    @Sinbad1999 4 роки тому +3

    Can you remake this video but in Windows please? It's really hard to follow you whilst you're using a different program than Visual Studio.