# Tag Info

41

It's not just about speed of execution, but also about simplicity. Although the software rendering used in this example would be a lot slower than using hardware acceleration (i.e. a GPU), drawing a few bitmaps on screen is such a trivial task that you would not notice the performance drop. However, low-level activity like triangle rasterisation, depth sort ...

25

My question is: why even bother using something like open gl, sfml, sdl when all you have to do is simply allocate some buffer, pass a bitmap and draw it to the screen? Short: Because its fast (OpenGL, DirectX). Long: You may think you can do this all yourself. Draw pixels to a screen. You might write a small library to draw shapes, like quads or ...

20

When reading the code-example provided by SDL on the SDL_GetPerformanceFrequency I could easily find out how you could calculate a deltatime using these functions. It should basically come down to this for you: Uint64 NOW = SDL_GetPerformanceCounter(); Uint64 LAST = 0; double deltaTime = 0; while (somebool) { LAST = NOW; NOW = ...

15

If your frame time is unpredictable (whether or not this is your fault; the OS may be using resources occasionally, etc), capping to a predictable frame rate that is somewhat lower than your achievable framerate will do a lot for predictable latency, which can make the game feel a lot better. Changing how long between when you process input and render ...

12

You will end up using a lot less CPU (multi taskers will thank you) and people on mobile devices will appreciate this, too.

11

What he does is called software rendering, what OpenGL does is called GPU rendering What's the difference between them? Speed and memory. Rasterization (filling out triangles on screen) takes some time. If you do it on the CPU, you essentially take that time away from game logic, especially if it's not optimized well. And doesn't matter, how small the ...

10

You should never, ever, ever, ever, ever use Sleep to control framerate. This has been gone over before and I'll refer you to the other question for discussion of the reasons why. One thing not mentioned there is that at the typical modern refresh rate of 60Hz, and with the typical 1 millisecond granularity of Sleep calls, it's actually impossible to Sleep ...

10

SDL2 doesn't need any functionality to be added for either of those items. Texture batching You yourself can sort the sprites by texture used. The SDL backends can already do draw call batching if they wish to (nothing requires that the Copy command be executed immediately; the only requirement is that it be executed by the time any side effects are ...

9

On tile based GPUs (commonly seen in mobile hardware) it is beneficial to clear the renderer, as it allows the GPU to dispose of the existing cached frame data. Here's a quote from OpenGL Insights, dealing with the benefits of clearing the framebuffer on a tile based renderer: "On a tile-based architecture, avoiding clears can be disasterous for ...

9

Yes, it's possible. No its not a dumb idea. In fact many of the older racing games did something similar. Super Mario Kart is one example. The rendering uses 2d sprites in 3d space instead of polygons, but the physics engine is all 2d.

8

I know how you feel, SDL2 is somewhat different from the earlier one, to make it easier, I'll explain how SDL_Renderer, SDL_Window, and SDL_Texture works. //CREATE WINDOW SDL_Window *window = SDL_CreateWindow("Title", 100, 100, 400, 600,SDL_WINDOW_SHOWN); /*You have created a variable named window which is in the type of SDL_Window, SDL_Create Window ...

8

What you may wish to do in this case is check the timer when you started printing the text on the screen and decide on an amount of time that it will take to print each letter in the paragraph. Then each frame check the time and print this portion of the text: int charsToPrint = (time - started) / amount; if (amount < originalText.length()){ ...

8

While the answers from others are more correct than any answer I could give, I want to point out the fundamental misunderstanding about how software development works that I think underlies your question. While it's always possible to do things "by yourself" without a framework, and there's often great educational benefit from doing so, the reality is that's ...

7

The documentation says about SDL_PollEvent: Use this function to poll for currently pending events. And you are supposed to use it this way (still from the doc): while (1) { SDL_Event event; while (SDL_PollEvent(&event)) { // handle your event here } // do some other stuff here -- draw your app, etc. } In fact, you can have ...

7

As congusbongus notes in his answer, many games benefit from dividing content into "scenes". A scene might represent a particular level, a room, a minigame, or lots of other possibilities, depending on the game. In such a system, menus can be implemented as scenes as well, and this works well for simple games with only a few menus. Personally, I like to ...

7

It's not dumb, but realize that car/racing physics are very different from traditional game physics. You're not going to be able to just drop in Box2D. Traction, handling, acceleration, braking, etc. are all going to need a good deal of special casing. It'll likely be a lot easier sticking to 2D collision handling, of course, but collision is hardly going ...

7

SDL_Delay() doesn't really have fidelity of any sort; it puts your main thread to sleep, and once your thread is sleeping it's up to the OS to decide when to wake you up again, which could happen at any time. It certainly won't happen on a reliable schedule, or be guaranteed to be "within 1ms" of your nominated time or anything of the sort. It's this ...

7

There are couple of things you can do, if you haven't already: Use texture compression to decrease memory footprint. OpenGL supports this out of the box nowdays. Don't use 32 bit textures. Sometimes textures with lower bit-depth can look as good as the 32 bit versions. For example if you don't absolutely need very precise transparency, you could use ...

6

JPG is lossy. Don't use that for sprites -- you will end up with nasty artifacts that will look bad. There's a couple reasons you might want to use colur-keying, but they're a bit lost in todays hardware. Taking a quick look at color key advantages: Pros They use up less disk space -- there is no alpha channel to store By consequence, their memory foot ...

6

It depends on what level of physics realism you are aiming for. If you're fine with a Mario-kart type of racing game, then you won't have much trouble with a 2D physics engine. But if you want a top-heavy vehicle to roll over when it goes around a sharp turn at high speed, then you will need 3D physics.

6

It's pretty straightforward, if not entirely well documented. Reading the source (not sure if the released version differs significantly) tells me that you must export an environment variable SDL_DYNAMIC_API with the name of the .so you want to load. That .so must contain a symbol SDL_DYNAPI_entry with signature: Sint32(SDLCALL*)(Uint32 apiver, void *table, ...

6

So it seems I was missing this in my Game class, which makes sense: SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ); https://wiki.libsdl.org/SDL_HINT_RENDER_SCALE_QUALITY

6

You can't reliably control the FPS with functions like SDL_Delay, they call the operating sleep function, which tells the operating system "Please don't give me any CPU time for at least N miliseconds", the operating system is then free to decide: At what granularity (i.e., as you said you specify 1ms and it waits for 15ms, it has a granularity of 15ms) it ...

6

You can acheive what you want by drawing the text with an outline in one color, and then drawing it without outline on top of that, in another color. Here's an example of white text with a black outline from the libsdl forum: #define OUTLINE_SIZE 2 /* load font and its outline */ font = TTF_OpenFont(font_path, font_size); font_outline = TTF_OpenFont(...

6

Use a CMakeLists.txt file. It would look something like this. cmake_minimum_required(VERSION 3.3) project(projectname) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -pthread -fpermissive") find_package (PkgConfig REQUIRED) find_package (OpenGL REQUIRED) find_package (GLUT REQUIRED)...

6

SDL2's "texture" rendering API is designed for accelerated graphics in mind; this is where you upload textures to your graphics hardware beforehand, and then render that texture by referring to it. This is why textures are write-only, and can only be written to if: the texture was created with the SDL_TEXTUREACCESS_STREAMING flag you lock the texture ...

5

If you make the scale an integer number, like 2 here SDL_RenderSetScale(renderer, 2) then every pixel will scale up to an integer number of pixels. There's then no need for anti-aliasing between pixels and hence no blurriness.

5

The literal answer is actually yes because the Emscripten project, for compiling C++ applications to Javascript for running in browsers, provides an implementation: "libc and stdlibc++ support is very good. SDL support is sufficient to run quite a lot of code. OpenGL support is in very good shape for OpenGL ES 2.0-type code, and even some other types," in ...

5

You can use the SDL function SDL_SetEventFilter as detailed here: http://www.libsdl.org/release/SDL-1.2.15/docs/html/sdlseteventfilter.html Basically, it takes as a parameter a function to use as a filter which events will be passed through and then determined as valid or invalid. The function should return 1 if valid or 0 if invalid. For example, you ...

5

Here's my game loop: double lastTime = game->GetGameTime().TotalTime(); double timeBehind = 0.0; while( !end ) { // Update the game time. game->GetGameTime().Update(); // Update game time values. flagEnd = game->HasEnded(); // Ignore this. ...

Only top voted, non community-wiki answers of a minimum length are eligible