Aula 01
Desenvolvimento de Jogos com a Biblioteca SDL libsdl – Programação em C++
Summary
# Live Class Summary: Introduction to C++ and SDL
## Overview
In this live class, we explored programming with C++ using the SDL (Simple DirectMedia Layer) library, commonly used for game development.
## Key Points
- **Introduction to SDL:**
- SDL is a cross-platform library supporting various systems including Windows, Mac, Linux, iOS, and Android.
- The current version in use is SDL2.
- **Installation:**
- For Linux, you can install SDL using package managers like `apt`.
- For Windows and Mac users, binaries are provided on the download page.
- **Setup:**
- After installing SDL, you need to include SDL headers in your C++ program (e.g., `#include <SDL2/SDL.h>`).
- **Initialization:**
- SDL is initialized using `SDL_Init()` which requires specifying the subsystems needed, for example, video and audio.
- Proper error handling was demonstrated using `SDL_GetError()` to log any issues during initialization.
- **Creating a Window:**
- A window is created with `SDL_CreateWindow()`, which takes parameters such as title, position, dimensions, and flags.
- Error handling for window creation was discussed.
- **Rendering:**
- To change the background color, the program retrieves the window surface using `SDL_GetWindowSurface()`.
- The `SDL_FillRect()` function is used to fill the window surface with a specific color.
- The screen must be updated with `SDL_UpdateWindowSurface()` after painting.
- **Program Execution Control:**
- `SDL_Delay()` is used to pause the execution, allowing the window to remain visible for a set duration.
- **Cleanup:**
- It is important to release resources with `SDL_DestroyWindow()` and `SDL_Quit()` before exiting the program to prevent memory leaks.
## Conclusion
The session concluded with a recap of the SDL programming steps, emphasizing the importance of initializing, error handling, resource management, and the program's life cycle. Viewers were encouraged to like, comment, and subscribe for more content.
**Next Live Session:**
Stay tuned for the next live class to delve deeper into SDL functionalities!
Thank you for joining us at **NBK Mundo Tech**!
Video Transcript
Welcome to our live.
In this live class we will explore the programming in C++, C++, or Bbioteca SDL.
Bbioteca SDL, simple direct media layer, a Bbioteca used for game development.
Let's start exploring.
Let's play with SDL to see what happens.
Don't forget to leave your like, comment and subscribe to the channel.
We are coming.
Let's wait 30 seconds to start.
Let's start.
We are on the live.
If you want to take a look at the documentation, you can support several platforms.
Windows, Mac, Linux, iOS, Android, etc.
I will use the C++ to demonstrate.
The version 2 will be the most current version of SDL.
If you go to the WIC documentation, there is an introduction page.
This page shows how to install for several systems.
In my case I am using Linux, distribution, and Ubuntu.
For this you can download the source code, compile it, or if you have a package generator,
I will use the apt.
We can use the apt and install the name of the package.
In the case of Windows and Mac, you can also choose the instructions.
But just go to the download page.
There is the binary for Windows and the DMG for Mac.
I will open my terminal.
I will use the name of the package.
If I do a search, I will use the SDL2.
This package will serve.
This is what I will use.
I have the SDL2.
I have installed the SDL2.
It will be installed in my case.
In the case of the SDL2, it will install several files.
We will import the SDL.
This SDL.h will be the file we will use in our code.
You can see the SDL2.
You can see the SDL.h.
Here is the code.
It will include several things.
I will create a new file.
I will use the name of the SDL2.h.
I will use the name of the SDL2.
This is why it knows where to search.
This is the SDL2.h.
Let me save this file.
I will use the name of the SDL2.h.
Let's see if this works.
I will use the folder of the SDL2.h.
The folder of the SDL2.h.
It compiles something.
Let's continue.
For the SDL2, we have to do the following.
First, we start the system.
The function is SDL.h.
You can pass the sub-systems as the argument you want.
The sub-system list is here.
I only want to make a video, so I will use the SDL2.h.
If you want more than one, just use the OR or logic operator.
To combine all the sub-systems you want to start.
For example, if you want video and audio, just say the video and put the logic OR and the other audio.
Let's start the SDL2.h.
Now you pass the sub-systems you want.
I will use the SDL2.h.
Now this function returns something.
This will be the error value.
If there is no error, it returns 0.
But if there is no error, it returns 0.
That's why we have to do the if statement to deal with the error.
If the return value of the SDL is not 0, it means there is a problem.
Let's do something.
I will show the error.
We can use the SDL2.h.
It didn't work.
We can use the S to substitute the variable or the value we put as the following arguments.
In this case, I want this argument to be corresponding to the S.
This will be the S to get error function.
The S to get error function is a function you can call to obtain the error.
If you make any mistake, just call the S to get error and you will be able to show the error.
The S to get error will be called.
The error message will appear here and be replaced where the S has the percentage.
After this log, I will say return 1.
We will return the main function.
The return value 0 means there was no error.
Any value that is not 0 means an error.
The return value is the error code.
In this case, the error code is 1.
It can be anything you want it to be.
So 1 means that it gave the problem to the SDL.
If you have other errors in the app, you can use other numbers.
Obviously you can use a constant here to define these constant numbers when you organize your code.
Ready, start the SDL.
Now we won't do anything yet, but before you finish the program, you have to finish the SDL
to be able to clean everything and leave everything in the conformance.
So you call the SDL and close.
Sorry, it's quit.
Let me search here in wiki, the SDL quit.
It was there in the example.
So just call the SDL quit to be able to close.
So let's compile this.
It's there G++, now there's a problem here.
It says that the SDL init, getError, log, quit were not defined.
This is because we didn't link the linker.
We have to link with the library to do this in G++, just use the option traceL.
G++ main.cpp traceLsdl2.
A huge detail that I've already had a problem with is that this linker option only has to appear after the list of the source code files.
If you put it before it won't work, it will be a problem.
So if you don't have a header, put the option traceL after the .cpp.
With this it will generate the .out.
We can run, obviously it worked fine, but it doesn't do anything.
As we didn't have an error, we know that everything worked fine.
So let's do something more cool.
So in the SDL we will first do the following.
As an objective, we will show a screen and in this screen we will paint the background color to blue.
Let's do this. First we have to make a window screen.
So we will use the SDL createWindow function.
If you look for a wiki window, it has the function here, createWindow.
And this function takes this number of parameters here.
First we stop being the title, followed by the two, to the position of the window, followed by the two, to the dimensions, the width and height of the window.
And the end is one of the flags, a flag, it's called.
Since there is nothing special, it's zero for flags.
So let's go, let's go.
We will do the following, SDL createWindow, what is the title of the world.
The second argument will be the position of the window.
There is a constant here that we can use to decentralize the window.
And this constant is SDL windowPauseCenter, I will copy that.
This constant here, we can use for x and for y.
Let me give the...
...soft.
The line break.
And now the width and height, I will give the width 140, the height 480.
Flag I will put zero, because I have nothing.
Let me break the line here to get better at seeing.
It's too long.
Now this function is called return with value here.
Or return no, if it's a problem.
Or return a pointer to SDL window, you notice here that the value returns.
So let's do this.
Point for SDL window.
Let's call this variable window.
Now let's check by mistake if the window was no.
This is not right, create the window, we will do...
It can be a log, as we did earlier.
You can use the standard CR, whatever you want.
To illustrate the CR, I will use the wire stream, just so you know how to do it.
If you don't know this, more and more.
STD, two points, two points, is the namespace.
If you want, you can use the username, namespace, STD, to have to type this,
but I can type explicitly.
If the CR didn't work, create the window.
And I will use the space and add... How do I get the error?
Remember?
SDL get error.
And I will type STD, two points, and L to type the line.
And after that I will return 2, to indicate that 2 didn't work, create the window.
This is the error.
If everything works, after creating the window,
we will do the painting and update the window.
But let's see if it works this way.
More and more again, to compile, let's run.
So it worked, nothing too much, no problems.
So let's look at the create window, the example it has.
So when we create things, normally, I don't know, more and more, STD,
we allocate, I pass on the memory.
So when we finish the program, or are doing certain things that don't need more,
we have to allocate the memory, to be able to release that memory to other resources.
The way to do this is, for the window, in this case, it will be the destroy window.
So you will do, to clean everything, destroy window,
don't forget to do this to be able to clean and release the memory.
As we didn't see anything that was happening,
it's because it creates the window and immediately releases the program.
So to see what is really happening, we will put the so-called delay,
to be able to stop the program, pause a little.
So there is this STD delay function, we can use it,
it takes an argument that is the number of milliseconds,
to be 5 seconds, it has to be 5 times 1000, 5,000.
So it will stop the program for 5 seconds and we will see the screen.
Let's go there, click there and run.
And my screen appeared on the other monitor.
So it's here, wait a minute, it's opening on the other one.
So this is the window that appears.
So the black window, nothing.
So let's learn how to change the background color of this window.
So we can use the fill rack function, let's look here.
So this STD delay was the rack we can use to paint a rectangle.
The way to do this is to have a surface, a surface.
We will use the surface of the screen.
And then we will use a color to make the color map,
you use STD-L-MAP-RGB.
To be able to get the surface of the screen, there is a GET-WINDOW-SURFACE.
This function here that returns a point of the surface and the window argument,
point to the window. So let's use that.
So after having the window, we will get the surface of the window,
get-windowsurface, pass the point to the window and from that we will get the point to the surface,
surface.
With that we have the surface of the window, we can use this to change the color,
to paint a rectangle of blue color to cover the whole window.
And we will use that fill rack function that we saw before.
So let's go.
So STD-L fill rack, but what is the surface?
It will be the window surface that I didn't give the name of the variable,
let me give it here, I'll call it window surface, window surface.
Now the second argument, let's see here.
The structure is STD-L-Rack, a rectangle representing the rectangle to be able to paint,
or NO to be able to paint the entire surface.
So NO will paint the entire surface and the color, what is the color?
We can use STD-L, map RGB, this function has to give the shape of the surface.
So window surface, we will reference the biggest sign,
format, format of the surface and now you give the RGB,
followed by, separated by the Vigol, arguments.
What is the argument for R, red, red?
I will give the red 0, what is green?
What is the blue?
It will be 255 to be able to be totally blue.
Let's see, let's see.
More, more.
So nothing happened, why?
So we painted the surface of the screen, but the screen is not updated.
So we have to update the screen.
So to be able to do this, we will have to have a function to update the screen.
Let me look here.
Let me look here.
To have this function update window surface, let's see if it works.
After painting the surface, we will update the window surface.
Pass the pointer to the window, window and let's see what it gives.
Here it is, and now it worked.
Then the window, a blue screen, and here it is the world as the title.
So let's review what we learned here.
So, SDR, a library that we can use a lot in games development.
Let's use version 2.
I'm using I don't know more, but you can use C and there are several binds to other languages, just look on the website.
And we started SDR with SDR in net, pass the subsystems that you want, subsystems.
SDR in net returns value no 0, if it is a problem.
You can use SDR and log to log something to the screen.
To get the error message, you use SDR get error.
First, we create the window, SDR create window,
Amazon variable window, which is the pointer value for SDR and window.
SDR create window returns no SDR, that's why we deal with the error here.
After creating the window, we get the surface of the window with SDR get window surface.
Get window surface returns pointer to SDR surface, surface,
to be able to paint the window with a blue rectangle covering the entire window, we can use SDR fill rack.
Get the surface that we want to paint.
I want a new one for the entire surface, but if you want only part, you can use SDR Rack.
You can paint the colors, calling the function SDR map RGB, you have to pass the surface format,
if you want to map this RGB color.
In this case, I used the window surface, reference with this larger line, the format attribute,
and you pass the RGB value, in this case, R0G0B255, because it's completely blue.
To be able to see what happened, you use the SDR update window surface,
to be able to update the surface of the screen, you pass the argument, the pointer and the window.
As this is already closing the program, nothing is happening too much, we use the SDR delay to be able to pause the execution,
for 5 seconds to see what really happened.
Whenever we set the memory, for example, we set the memory, we create a window, we have to clean things.
In this case, I called the destroy window to be able to destroy the window that was set.
There is also this window surface, normally when you create the surface, you have to use it,
call the function to release the memory that was used by the surface,
but I think the destroy window already deals with the release of the window surface,
so you don't need to explicitly release the window surface.
After that, before the program leaves, you have to call the SDR quit to be able to turn off the SDR,
the system will release the memory and so on.
Don't forget that.
Okay, so that's it for this live, in the next live, if you're old, we'll talk more about SDR.
Don't forget to give us a like, comment, subscribe to our channel,
NBKMundoTech, don't forget to subscribe and thanks for watching, see you next time.
Nenhum comentário ainda (loading...)
Nenhum comentário ainda (loading...)
Gostou da aula? 😆👍
Apoie nosso trabalho com uma doação: