diff --git a/loki_sample/direct_pixel_access_example/direct-pixel-drawing-sdl.c b/loki_sample/direct_pixel_access_example/direct-pixel-drawing-sdl.c new file mode 100644 index 0000000..cb79bb2 --- /dev/null +++ b/loki_sample/direct_pixel_access_example/direct-pixel-drawing-sdl.c @@ -0,0 +1,80 @@ +/* Example of direct pixel access with SDL. */ + +#include +#include +#include + +Uint16 CreateHicolorPixel(SDL_PixelFormat * fmt, Uint8 red, + Uint8 green, Uint8 blue) + +{ + Uint16 value; + + /* This series of bit shifts uses the information from the SDL_Format structure + * to correctly compose a 16-bit pixel value from 8-bit red, green, and blue data + */ + value = ((red >> fmt->Rloss) << fmt->Rshift) + + ((green >> fmt->Gloss) << fmt->Gshift) + + ((blue >> fmt->Bloss) << fmt->Bshift); + + return value; +} + +int main() +{ + SDL_Surface *screen; + Uint16 *raw_pixels; + int x, y; + + /* Initialize SDL's video system and check for errors. */ + if (SDL_Init(SDL_INIT_VIDEO) != 0) { + printf("Unable to initialize SDL: %s\n", SDL_GetError()); + return 1; + } + + atexit(SDL_Quit); + + /* Attempt to set a 256x256 hicolor */ + screen = SDL_SetVideoMode(256, 256, 16, 0); + if (screen == NULL) { + printf("Unable to set video mode: %s\n", SDL_GetError()); + return 1; + } + + /* Video memory can be strange, and it's sometimes necessary + * to "lock it before it can be modified, SDL abstracts this + * with the SDL_LockSurface function. */ + SDL_LockSurface(screen); + + /* Get a pointer to the video surface's memory. */ + raw_pixels = (Uint16 *) screen->pixels; + + /* We can now safely write to the video surface. We'll draw + * a nice gradient patern by varying our red and blue components + * along the X and Y axes. Notice the forumula used to calculate + * the offset into the framebuffer for each pixel. + * (The pitch is the number of bytes per scanline in memory.) */ + + for (x=0; x < 256; x++) { + for (y=0; y < 256; y++) { + Uint16 pixel_color; + int offset; + pixel_color = CreateHicolorPixel(screen->format, + x, 0, y); + offset = (screen->pitch /2 * y + x); + raw_pixels[offset] = pixel_color; + } + } + /* We're finished drawing, so unlock the surface. */ + SDL_UnlockSurface(screen); + + /* Inform SDL that the screen has been chnaged. This is + * necessary because SDL's screen surface is not always the real + * framebuffer; it is sometimes emulated behind the scenes. */ + SDL_UpdateRect(screen, 0, 0, 0, 0); + + /* Pause for a few seconds as the viewer gasps in awe. */ + SDL_Delay(3000); + + return 0; +} diff --git a/loki_sample/init_sdl_example/initializing-sdl.c b/loki_sample/init_sdl_example/initializing-sdl.c new file mode 100644 index 0000000..0d45601 --- /dev/null +++ b/loki_sample/init_sdl_example/initializing-sdl.c @@ -0,0 +1,32 @@ +/* Example of initializing SDL. */ + +#include +#include +#include + +int main() +{ + SDL_Surface *screen; + + /* Initialize SDL's video system and check for errors */ + if (SDL_Init(SDL_INIT_VIDEO) != 0) { + printf("Unable to initialize SDL: %s\n", SDL_GetError()); + return 1; + } + + /* Make sure SDL_Quit gets called when the program exits! */ + atexit(SDL_Quit); + + /* Attempt to set a 650x480 hicolor video mode */ // I uped it a bit cuz fuck that. + screen = SDL_SetVideoMode(1024, 768, 32, SDL_FULLSCREEN); + if (screen == NULL) { + printf("Unable to set video mode: %s\n", SDL_GetError()); + return 1; + } + + /* If we got this far, everything worked */ + printf("Success!\n"); + + return 0; + +} diff --git a/loki_sample/notes b/loki_sample/notes index bb8af32..179d6e5 100644 --- a/loki_sample/notes +++ b/loki_sample/notes @@ -18,3 +18,7 @@ main loop >> Just a while loop that runs the entire time anywhere from 30 to 60 seperating meu system from main loop is good, but can complicate the code Networking information should usually be grabed first Graphics should be usually grabed last + +-I/usr/include/SDL -D_GNU_SOURCE=1 -D_REENTRANT +-lSDL -lpthread +