Close

Hello World

A project log for ChassC

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

morningstarMorning.Star 02/19/2018 at 09:340 Comments

As I said, the fonts are a pile of poo on my machine so no text as yet.

Instead, we'll open a graphic window and draw a square in it. Really easy...

SDL_Rect rectangle;
rectangle.x=100;
rectangle.y=100;
rectangle.w=100;
rectangle.h=100;

SDL_SetRenderDrawColor(render,255,255,192,255);
SDL_RenderClear(render);

SDL_SetRenderDrawColor(render,0,0,0,255);

SDL_RenderFillRect(render, &rectangle)

SDL_RenderPresent(render);

First you'll need a rectangle to draw.  SDL provides a declared type for this, using the coordinates of the topleft corner and a width and height. Its a 100x100 square at (100,100).

Next clear the screen to my favourite pastel yellow background with SetRenderDrawColour, using Red Green Blue and Alpha components. Setting Alpha to 255 makes the shape fully opaque, 0 makes it fully transparent.

Change the draw colour to black for the next part, the rectangle.

Draw the rectangle on the render buffer with RenderFillRect. There is also a RenderDrawRect function that draws an empty rectangle.

And finally, RenderPresent draws the render buffer on the display, the window will be blank until this point. This fills the window border-to-border with the image, which is the same size as the window.

SDL can also draw single pixels with

SDL_Point pt;
pt.x=100;
pt.y=100;

SDL_SetRenderDrawColor(render,255,0,0,255);
SDL_RenderDrawPoint(render,pt.x,pt.y);

 and single pixel lines with

SDL_RenderDrawLine(render,x1,y1,x2,y2);

 specifying the ends of the line like so.

All three commands can also be called with an s on the end, and an array of coordinates to draw.

SDL_Point pt[100];

for (int n=0; n<100; n++) {
  pt[n].x=100+n;
  pt[n].y=100+n;
}
SDL_SetRenderDrawColor(render,255,0,0,255);
SDL_RenderDrawPoints(render,pt,100);

 This will draw a diagonal line of points on the window in one operation.

Discussions