Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* compile with -lSDL2 */
- #include <SDL2/SDL.h>
- #include <errno.h>
- #include <stdint.h>
- #include <stdlib.h>
- #include <string.h>
- enum {
- WIDTH = 320,
- HEIGHT = 240,
- PFORMAT = SDL_PIXELFORMAT_ARGB8888,
- };
- static inline Uint32 rgb(Uint8 r, Uint8 g, Uint8 b)
- {
- static SDL_PixelFormat *pf = NULL;
- Uint32 ret;
- if (pf == NULL) {
- pf = SDL_AllocFormat(PFORMAT);
- if (pf == NULL) {
- fprintf(stderr, "Could not allocte pixel format: %s\n",
- SDL_GetError());
- exit(EXIT_FAILURE);
- }
- }
- ret = SDL_MapRGB(pf, r, g, b);
- SDL_FreeFormat(pf);
- return ret;
- }
- int main(void)
- {
- SDL_Window *sw;
- SDL_Renderer *sr;
- SDL_Texture *st;
- Uint32 *buffer;
- if (SDL_Init(SDL_INIT_VIDEO) < 0) {
- fprintf(stderr, "Could not initialise SDL: %s\n", SDL_GetError());
- return EXIT_FAILURE;
- }
- sw = SDL_CreateWindow("Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
- WIDTH, HEIGHT, 0);
- if (sw == NULL) {
- fprintf(stderr, "Could not create window: %s\n", SDL_GetError());
- return EXIT_FAILURE;
- }
- sr = SDL_CreateRenderer(sw, -1, SDL_RENDERER_PRESENTVSYNC);
- if (sr == NULL) {
- fprintf(stderr, "Could not create renderer: %s\n", SDL_GetError());
- return EXIT_FAILURE;
- }
- st = SDL_CreateTexture(sr, PFORMAT, SDL_TEXTUREACCESS_STREAMING, WIDTH, HEIGHT);
- if (st == NULL) {
- fprintf(stderr, "Could not create texture: %s\n", SDL_GetError());
- return EXIT_FAILURE;
- }
- buffer = malloc(WIDTH * HEIGHT * sizeof *buffer);
- if (buffer == NULL) {
- fprintf(stderr, "Could not allocate memory: %s\n", strerror(errno));
- return EXIT_FAILURE;
- }
- for (int x = 0; x < WIDTH; x++)
- for (int y = 0; y < HEIGHT; y++)
- buffer[x + WIDTH * y] = rgb(rand(), rand(), rand());
- SDL_UpdateTexture(st, NULL, buffer, WIDTH * sizeof *buffer);
- SDL_RenderCopy(sr, st, NULL, NULL);
- SDL_RenderPresent(sr);
- int shouldquit = 0;
- SDL_Event se;
- while (!shouldquit) {
- if(0 == SDL_WaitEvent(&se)) continue;
- if (se.type == SDL_QUIT)
- shouldquit = 1;
- }
- SDL_DestroyTexture(st);
- SDL_DestroyRenderer(sr);
- SDL_DestroyWindow(sw);
- free(buffer);
- SDL_Quit();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement