How to draw pixels in SDL 2.0?

How to draw pixels in SDL2.0?

I am trying to get to know C ++, but it is very difficult to do without beautiful photos, so I am trying to launch a very simple graphic display. All I really want is to give me a window, let me draw rgbΞ± pixels on it and gain access to information about these pixels. There may be other things that I want that I don’t know about, but that’s all there is on my list right now. My research on this led me to try using SDL, the current version 2.0.

Almost all of my graphics capabilities come from using JavaScript on <canvas> . Most of the other bit comes from my calculator, which has this awesome Pxl-On() command, so simple.

I use MinGW for my C ++, if that matters. Also, if there is something better ** than SDL2.0 for what I need, advice is welcome.


** "better" means "contains what kind of functionality I need, but less complete functionality than SDL2.0, and / or has a more intuitive / less complicated *** API than SDL2.0."

*** Less lines of code to accomplish the same task.

+7
c ++ c sdl sdl-2
source share
3 answers

I do not know how your code is structured. Assuming you have SDL_Window and SDL_Renderer, you just need to call SDL_RenderDrawPoint(renderer, x, y) .

If you do not have a renderer or window, you can create both with SDL_CreateWindowAndRenderer (). For example:

 SDL_Window *window; SDL_Renderer *renderer; SDL_CreateWindowAndRenderer(800, 600, 0, &window, &renderer); //Probably on a loop SDL_RenderDrawPoint(renderer, 400, 300); //Renders on middle of screen. SDL_RenderPresent(renderer); 

This should draw a pixel in the middle of the screen. Reading a pixel is a little harder. You can use SDL_RenderReadPixels() , it is intended to read the area, but you can specify a 1x1 area. Read the wiki page if you really need it.

If you are having problems with SDL2, we recommend reading the Lazy Foo tutorials . The SDL2 section is still working, but there is enough material to start learning.

+8
source share

Runnable example

Draws a diagonal red line pixel by pixel on the screen using SDL_RenderDrawPoint .

enter image description here

main.c

 #include <stdlib.h> #include <SDL2/SDL.h> #define WINDOW_WIDTH 600 int main(void) { SDL_Event event; SDL_Renderer *renderer; SDL_Window *window; int i; SDL_Init(SDL_INIT_VIDEO); SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_WIDTH, 0, &window, &renderer); SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); SDL_RenderClear(renderer); SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); for (i = 0; i < WINDOW_WIDTH; ++i) SDL_RenderDrawPoint(renderer, i, i); SDL_RenderPresent(renderer); while (1) { if (SDL_PollEvent(&event) && event.type == SDL_QUIT) break; } SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit(); return EXIT_SUCCESS; } 

GitHub upstream .

Compile and run:

 gcc -std=c89 -Wextra -pedantic-errors -o main.out main.c -lSDL2 ./main.out 

If you want to set a large rectangle of pixels at once, for example, the whole screen or sprite, use SDL_Texture + SDL_RenderCopy and possibly SDL_TEXTUREACCESS_STREAMING , since it will be much faster. Examples on:

  • What is bleit in SDL?
  • Rendering pixels from an array of RGB values ​​in SDL 1.2?

Tested on libsdl 2.0.2, Ubuntu 15.10.

+8
source share

I find Python + PySDL2 simpler for prototype. Debug is also funny because it is a veeeery slooow for pixel graphics. =) Here is the full code:

 """ The code is placed into public domain by anatoly techtonik < techtonik@gmail.com > """ import sdl2 import sdl2.ext sdl2.ext.init() window = sdl2.ext.Window('', size=(300, 100)) window.show() renderer = sdl2.ext.Renderer(window) renderer.draw_point([10,10], sdl2.ext.Color(255,255,255)) renderer.present() running = True while running: for e in sdl2.ext.get_events(): if e.type == sdl2.SDL_QUIT: running = False break if e.type == sdl2.SDL_KEYDOWN: if e.key.keysym.sym == sdl2.SDLK_ESCAPE: running = False break 
+1
source share

All Articles