| 1 | //gcc -Wextra -pedantic-errors -o main.out main.c -lSDL2 |
| 2 | |
| 3 | #include <unistd.h> |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | |
| 7 | #include <SDL2/SDL.h> |
| 8 | |
| 9 | #define WINDOW_WIDTH 600 |
| 10 | |
| 11 | int main(void) { |
| 12 | SDL_Event event; |
| 13 | SDL_Renderer *renderer; |
| 14 | SDL_Window *window; |
| 15 | int i; |
| 16 | |
| 17 | SDL_Init(SDL_INIT_VIDEO); |
| 18 | SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_WIDTH, SDL_WINDOW_RESIZABLE, &window, &renderer); |
| 19 | //SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP); |
| 20 | //SDL_RenderSetScale(renderer, 2.0, 2.0); |
| 21 | SDL_SetWindowTitle(window, "Simple SDL paint"); |
| 22 | |
| 23 | |
| 24 | SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); |
| 25 | SDL_RenderClear(renderer); |
| 26 | SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); |
| 27 | |
| 28 | for (i = 0; i < WINDOW_WIDTH; ++i) |
| 29 | SDL_RenderDrawPoint(renderer, i, i); |
| 30 | |
| 31 | SDL_RenderPresent(renderer); |
| 32 | |
| 33 | int running = 1; |
| 34 | int mouseX, mouseY; |
| 35 | Uint32 fullscreen_status = 0; |
| 36 | |
| 37 | while(running) { |
| 38 | event.type = 0; |
| 39 | if(SDL_PollEvent(&event)) { |
| 40 | switch(event.type){ |
| 41 | case SDL_WINDOWEVENT: |
| 42 | break; |
| 43 | case SDL_QUIT: |
| 44 | running = 0; |
| 45 | break; |
| 46 | case SDL_KEYDOWN: |
| 47 | //case SDL_KEYUP: |
| 48 | printf("Key: %d\t%d\t%d\n", event.key.keysym.scancode, event.key.keysym.sym, event.key.keysym.mod ); |
| 49 | |
| 50 | if(event.key.keysym.scancode == SDL_SCANCODE_F && event.key.keysym.mod == KMOD_LCTRL) { |
| 51 | fullscreen_status = fullscreen_status ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP; |
| 52 | SDL_SetWindowFullscreen(window, fullscreen_status); |
| 53 | } |
| 54 | |
| 55 | if(event.key.keysym.scancode == SDL_SCANCODE_Q && event.key.keysym.mod == KMOD_LCTRL) { |
| 56 | running = 0; |
| 57 | } |
| 58 | |
| 59 | break; |
| 60 | case SDL_MOUSEBUTTONDOWN: |
| 61 | //do whatever you want to do after a mouse button was pressed, |
| 62 | // e.g.: |
| 63 | //if(event.button.button == SDL_BUTTON_LEFT) { |
| 64 | while(event.type != SDL_MOUSEBUTTONUP) { |
| 65 | SDL_GetMouseState(&mouseX, &mouseY); |
| 66 | SDL_RenderDrawPoint(renderer, mouseX, mouseY); |
| 67 | SDL_RenderDrawPoint(renderer, mouseX, mouseY+1); |
| 68 | SDL_RenderDrawPoint(renderer, mouseX+1, mouseY); |
| 69 | SDL_RenderDrawPoint(renderer, mouseX+1, mouseY+1); |
| 70 | SDL_RenderPresent(renderer); |
| 71 | SDL_PollEvent(&event); |
| 72 | } |
| 73 | printf("Click: %d\t%d\t%d\n", mouseX, mouseY, event.button.button); |
| 74 | //} |
| 75 | break; |
| 76 | } |
| 77 | } |
| 78 | usleep(10000); |
| 79 | } |
| 80 | SDL_DestroyRenderer(renderer); |
| 81 | SDL_DestroyWindow(window); |
| 82 | SDL_Quit(); |
| 83 | return EXIT_SUCCESS; |
| 84 | } |