Giter Club home page Giter Club logo

sdl2-stb_image-example's Introduction

Loading images in SDL2 with stb_image

This example goes over how to loading images in SDL2 via stb_image.

Driver function:

#ifndef STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#endif // #define STB_IMAGE_IMPLEMENTATION
#include <SDL2/SDL.h>
#include <SDL2/SDL_pixels.h>
#include <SDL2/SDL_surface.h>
#include <SDL2/SDL_rect.h>

SDL_Surface *load_image(const char *filename) {
  Sint32 width = 0;
  Sint32 height = 0;
  Sint32 bytes_per_pixel =
      0; // Since we work with bytes instead of bits here in C.

  // Now the good stuff:
  Uint8 *data = stbi_load(filename, &width, &height, &bytes_per_pixel, 0);

  // What is a "pitch"? Its the "Dot Pitch" of an computer image.
  // Taken from https://en.wikipedia.org/wiki/Dot_pitch
  // Dot pitch (sometimes called line pitch, stripe pitch, or phosphor pitch) is
  // a specification for a computer display, computer printer, image scanner, or
  // other pixel-based devices that describe the distance, for example, between
  // dots (sub-pixels) on a display screen.
  Sint32 dot_pitch = width * bytes_per_pixel;
  dot_pitch = (dot_pitch + 3) & ~3;

  Sint32 red_mask;
  Sint32 green_mask;
  Sint32 blue_mask;
  Sint32 alpha_mask;

  // This only works with little endian computer processors,
  // if you want support for big endian (if you even still use it), implement it
  // yourself.
  red_mask = 0x000000FF;
  green_mask = 0x0000FF00;
  blue_mask = 0x00FF0000;
  alpha_mask = (bytes_per_pixel == 4) ? 0xFF000000 : 0;

  // Now finally we can get that damn surface.
  SDL_Surface *surface = SDL_CreateRGBSurfaceFrom(
      data, width, height, bytes_per_pixel * 8, dot_pitch, red_mask, green_mask,
      blue_mask, alpha_mask);
  if (!surface) {
    SDL_free(data);
    return NULL;
  }

  return surface;
}

sdl2-stb_image-example's People

Contributors

blastlessanthony avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.