OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 7:57 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Share code to draw using SDL
PostPosted: Tue Aug 08, 2017 3:37 pm 
Offline
Member
Member
User avatar

Joined: Tue Mar 06, 2007 11:17 am
Posts: 1225
So far, I use Windows, and the MSYS 1.0 that comes with MinGW to compile.

I can compile this SDL skeleton (with SDL 1, most surely also with SDL 2):
Code:
#include <SDL.h>

int main( int argc, char* args[] )
{
//Start SDL
///
  SDL_Init( SDL_INIT_EVERYTHING );

//Quit SDL
///
  SDL_Quit();

return 0;
}




This video shows how to compile it:


Quote:
http://lazyfoo.net/SDL_tutorials/lesson01/windows/mingw/index.php

Download SDL 1:
https://www.libsdl.org/download-1.2.php

Commands:
SDLCFLAGS=$(sdl-config --cflags)
SDLLIBS=$(sdl-config --libs)
gcc HelloSDL.cpp -o HelloSDL.exe $SDLCFLAGS $SDLLIBS
HelloSDL.exe




Now I want to know how I can create a window and draw generic stuff, like pixels, to the SDL window.

Can you share some clear code so I can do that, just creating the SDL window and drawing in it?

_________________
Live PC 1: Image Live PC 2: Image

YouTube:
http://youtube.com/@AltComp126/streams
http://youtube.com/@proyectos/streams

http://master.dl.sourceforge.net/projec ... 7z?viasf=1


Top
 Profile  
 
 Post subject: Re: Share code to draw using SDL
PostPosted: Tue Aug 08, 2017 6:16 pm 
Offline
Member
Member
User avatar

Joined: Sat Dec 27, 2014 9:11 am
Posts: 901
Location: Maadi, Cairo, Egypt
~ wrote:
Can you share some clear code so I can do that, just creating the SDL window and drawing in it?

The SDL documentation isn't that bad. Other SDL examples/tutorials aren't bad either and serve as a proper starting point.

Anyways...
Code:
SDL_Window *window;
SDL_Surface *surface;
uint32_t *framebuffer;      // pointer to raw pixel buffer that you can read/write

int init()
{
   // initialize the video subsystem
   if(SDL_Init(SDL_INIT_VIDEO) < 0)
      return 1;

   // make a window
   SDL_Window *window;
   window = SDL_CreateWindow("Window title", x_position, y_position, width, height, SDL_WINDOW_SHOWN);

   // ensure window handle is valid
   if(!window)
      return 1;

   // a surface is a group of pixels displayed on screen
   // return the window's surface
   SDL_Surface *surface;
   surface = SDL_GetWindowSurface(window);

   // ensure compatible surface
   // here, we're looking at a 32-bit little endian RGB buffer
   // basically, the bytes should be BB GG RR 00
   if(surface->format->BitsPerPixel != 32 || surface->format->BytesPerPixel != 4 || surface->format->Rmask != 0xFF0000 || surface->format->Gmask != 0x00FF00 || surface->format->Bmask != 0x0000FF)
      return 1;

   // set up the pointer to the framebuffer
   framebuffer = surface->pixels;

   while(1)
   {
      handle_events();
   }
}

// sample put pixel, assumes 32-bit little endian RGB as we verified above^^

void put_pixel(short x, short y, uint32_t color)
{
   // calculate pixel offsets just like any other graphics programming
   uint32_t *ptr = ((y * (width << 2)) + (x << 2) + framebuffer);

   // to modify pixels yourself, you need to lock the surface to be safe
   SDL_LockSurface(surface);

   ptr[0] = color;      // write the pixel

   // redraw
   SDL_UnlockSurface(surface);
   SDL_UpdateWindowSurface(window);
}



Notice the function "handle_events" must handle the SDL events. SDL, in my experience, doesn't let the window display any changes unless its events are being handled, which makes sense, really.

Of course, you don't need to verify a 32-bit little-endian RGB surface, but I just do it this way out of laziness. Instead, you can implement support for multiple surface formats, to ensure maximum compatibility.

EDIT: I'm not sure if this works for SDL1, as I only ever used SDL2.

_________________
You know your OS is advanced when you stop using the Intel programming guide as a reference.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 28 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group