Close

Blinking Cursor

A project log for ChassC

As in 'Chassis for C' Simple Direct Media Layer (SDL2) Template for C++

morningstarMorning.Star 02/19/2018 at 08:030 Comments

One of the reasons I like Python is Pygame, coincidentally written in SDL. It allows me to open up a graphics window, instead of boring console based stuff. I love the terminal, but sometimes a mouse is the only way. Doing that in C++ used to be a nightmare before I learned SDL. Interfacing to X directly is densly documented, I'm digging into that but its pretty much Linux-only research.

This should work on anything...


First of all, you'll need a working compiler.

sudo apt-get install g++

GNU C++11. I dream in this sometimes, recommended. ;-) 

Next you'll need a copy of the SDL Library and at least the SDL_Image library, with development files.

apt-cache search libsdl

libsdl2-2.0-0 - Simple DirectMedia Layer
libsdl2-dbg - Simple DirectMedia Layer debug files
libsdl2-dev - Simple DirectMedia Layer development files
libsdl2-doc - Reference manual for libsdl2

libsdl2-image-2.0-0 - Image loading library for Simple DirectMedia Layer 2, libraries
libsdl2-image-dbg - Image loading library for Simple DirectMedia Layer 2, debugging symbols
libsdl2-image-dev - Image loading library for Simple DirectMedia Layer 2, development files
libsdl2-mixer-2.0-0 - Mixer library for Simple DirectMedia Layer 2, libraries
libsdl2-mixer-dbg - Mixer library for Simple DirectMedia Layer 2, debugging
libsdl2-mixer-dev - Mixer library for Simple DirectMedia Layer 2, development files

libsdl-ttf2.0-0 - TrueType Font library for Simple DirectMedia Layer 1.2, libraries
libsdl-ttf2.0-dev - TrueType Font library for Simple DirectMedia Layer 1.2, development files

You'll need at least

sudo apt-get install libsdl2-2.0-0 libsdl2-dbg libsdl2-dev libsdl2-doc

and

sudo apt-get install libsdl2-image-2.0-0 libsdl2-image-dbg libsdl2-image-dev

to get a minimal working installation.

Now for the fun part.


C++ doesnt have many prerequisites to just jump in, there are a few tho. CIN, COUT and the SDL library, plus tell the compiler I'm too std::lazy to type classes, I'm not using them.

#include <iostream>
#include <SDL2/SDL.h>

using namespace std;

 Some basic parameters and somewhere to put stuff and we're off.

#define W 1024
#define H 768

SDL_Window * outputwindow = NULL;
SDL_Renderer * render = NULL;

outputwindow * stores the structure to the window instance, feeds user input back to the program and hosts the renderer.

render * stores the actual display, it is this you'll be writing to.

int main() {

  outputwindow = SDL_CreateWindow("ChassC", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, W, H, 0);
  render = SDL_CreateRenderer(outputwindow, -1, SDL_RENDERER_ACCELERATED);

  SDL_Delay(3000);

  SDL_DestroyWindow(outputwindow);
  SDL_Quit();

}

There are other options to opening a window, it can be borderless and fill the display, plus SDL handles multiple displays nicely, and can jump a window from one display to another easily.

Running that code will briefly open a graphical window before exiting gracefully.

Discussions