Advertisement

On learning to build a platform game engine with C and SDL2

Started by October 20, 2018 11:50 PM
3 comments, last by Alberth 6 years, 1 month ago

Hello,

I'm new here, I'm currently taking a 2 and a half year college course on programming. We're going through C algorithms, C# basics because we just began with C# and some Unix/Linux stuff. We're at the 2nd year of college, previously to that we mostly studied Linear Algebra, Boolean Algebra, and Statistics for the mathematical foundation of programming and we learned some C.

At the end of the year, I'll have to have the final class project ready. I've decided to build my own 2d platform game from scratch, using only the C language and SDL2 to make the job easier. I'm a bit of a Mario fanatic, I have all of the Mario games available on the Nintendo 3DS I own so I'll want to make it similar to Mario.

I'll be building it on Visual Studio community but I want the code to be portable enough to be easy to compile it on Linux and MacOS so as to expand on the project in the future.

Can I get some advice on this, please? How do I start? I already know some C but I haven't done any 2d programming, closest I came was making lines and squares with an opengl library called glut.h at in a recent college course on graphics programming. It didn't involve SDL.

 

Thank you,

I'd say start with a simple game like tetris, so you got to solve SDL2, keyboard or mouse, timing, winning, 2d, handling images, and a lot of C :) and code organizing.

A simpler game is 15-puzzle (no timing involved), quite more difficult is eg pipemania (it has animation and timers). The latter is likely a good second or even third game.

The game is not the point, it's just so you have a goal, without getting it in the way of programming (You don't have to spend time working out the game mechanics).

Pretty much everything on the screen are images that you render rather than lines and squares (the main difference is that you say "draw this image here" rather than "draw a square here"). For images, you can look for some free assets, or make a collection of images yourself.

 

As a side-note, watch out with brand names like Mario. Companies not only bring out games, they also own all names of it and its characters, and they are very protective with those names!! Don't name anything you make using a name that you don't own. You'll get into legal trouble very quickly.

Advertisement

Thank you for answering Alberth.

I'm aware of how video game franchise licenses work, I also aware of how overly strict Nintendo are with their franchises. Sega seems to be a bit more permissive when it comes to Sonic fan games.

I'm more interested in the mechanics and the tech behind platform games and how the graphics work.
I don't intend to use any franchise names.

Do you know any good programming resources for learning SDL and C for game programming? Maybe a few articles from here or another website?

I was just warning you in case you didn't know.

As for tech, conceptually a game is not very complicated. Input is keyboard/joystick/mouse. Output is an animation, a sequence of full-screen images generated at a 20-50 fps. You have a number of different game-parts (intro, high score, instructions, market (for selecting weapons, oil, tires), and at least one for actual playing a level ? )

Each game-part does something like


setup_data()

# game loop
while not done:
    render_current_situation_to_screen()
    input = get_user_input() # can be empty if user didn't do anything
    update_situation(input)  # could also use time here to move things that have non-zero speed
end

where at the end you have to select a new game-part to perform next. You likely also want to update higher level data structures with results or settings (like currently selected goodies, played levels with results, new score, etc.)

To generate a full-screen image, you typically load many small images in the setup step, and you draw them many times at the appropriate points in the screen. For tetris, you could have 7x20 grid of squares, where the data says what each cell contains (empty or 'square of block x'). You load an image of an empty square and an image of a full square (with different colours for different blocks) during setup(), and you render the grid of using the correct image as indicated in the data. In update, you move all the non-empty cells one level down every now and then, and when you render it again, it looks like the blocks have fallen a level.

SDL has a home-page libsdl.org, with the SDL function reference documentation. It also points at various other resources like tutorials. When I learned SDL1.2, I used the LazyFoo tutorial. Today you'll want to learn SDL2 instead. LazyFoo has that too.

I don't think "C for game programming" is a thing, it's C code, except you need more. Maybe you want you look into how to write larger programs. To be honest, I don't think C is a good choice, but ymmv.

This topic is closed to new replies.

Advertisement