Advertisement
LordJakub

Untitled

Feb 5th, 2024
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.35 KB | None | 0 0
  1. #include <SDL.h>
  2. #include <SDL_keycode.h>
  3. #include <stdio.h>
  4. #include <stdbool.h>
  5. #include <math.h>
  6. #include <SDL_image.h>
  7. SDL_Window* window ;
  8. SDL_Renderer* renderer;
  9. SDL_Event event;
  10.  
  11. int gInit(){
  12.     return SDL_Init(SDL_INIT_VIDEO);
  13. }
  14. int gCreateWindow(const char* title, int w,  int h){
  15.     window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, SDL_WINDOW_SHOWN);
  16.     renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
  17.     if (window == NULL) {
  18.         printf("Window error: ",SDL_GetError());
  19.         return 1;}
  20.     if (renderer == NULL) {
  21.         printf("Renderer error: ",SDL_GetError());
  22.         return 1;}
  23.     return 0;
  24. }
  25.  
  26. void gSetColor(int r, int g, int b, int a){
  27.     SDL_SetRenderDrawColor(renderer, r, g, b, a);
  28. }
  29. void gClear(){
  30.     SDL_RenderClear(renderer);
  31. }
  32. void gClose(){
  33.     SDL_DestroyRenderer(renderer);
  34.     SDL_DestroyWindow(window);
  35.     SDL_Quit();
  36. }
  37. void gQuit(){
  38.     SDL_Quit();
  39. }
  40.  
  41. void gCreateFillRect(int x, int y, int w, int h){
  42.     SDL_Rect rect = {x, y, w, h};
  43.     SDL_RenderFillRect(renderer, &rect);
  44. }
  45. void gUpdate(){
  46.     SDL_RenderPresent(renderer);
  47. }
  48. void gCreateRect(int x, int y, int w, int h){
  49.     SDL_Rect rect = {x, y, w, h};
  50.     SDL_RenderDrawRect(renderer, &rect);
  51. }
  52. void gCreateLine(int x1, int y1, int x2, int y2){
  53.     SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
  54. }
  55. void gCreateCircle(int x, int y, int r){
  56.     for (int i = 0; i < 360; i++){
  57.         SDL_RenderDrawPoint(renderer, x + r * cos(i), y + r * sin(i));
  58.     }
  59. }
  60. void gCreatePoint(int x, int y){
  61.     SDL_RenderDrawPoint(renderer, x, y);
  62. }
  63.  
  64. SDL_Texture* gLoadTexture(const char* path) {
  65.     SDL_Surface* surface = IMG_Load(path);
  66.     if (!surface) {
  67.         printf("Unable to load image %s! SDL_image Error: %s\n", path, IMG_GetError());
  68.         return NULL;
  69.     }
  70.     SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
  71.     if (!texture) {
  72.         printf("Unable to create texture from %s! SDL Error: %s\n", path, SDL_GetError());
  73.         return NULL;
  74.     }
  75.     SDL_FreeSurface(surface);
  76.     return texture;
  77. }
  78.  
  79. void gCreatePicture(const char* path, int x, int y, int w, int h) {
  80.     SDL_Texture* texture = gLoadTexture(path);
  81.     if (!texture) {
  82.         printf("Unable to load texture image on path %s!\n", path);
  83.          return;
  84.     }
  85.    
  86.     SDL_Rect destRect = { x, y, w, h };
  87.     SDL_RenderCopy(renderer, texture, NULL, &destRect);
  88.     SDL_DestroyTexture(texture);
  89. }
  90.  
  91.  
  92. bool gKeyPressed(const char *key) {
  93.     const Uint8 *keystate = SDL_GetKeyboardState(NULL);
  94.     //printf("Stisknuta klávesa: %s\n", key);
  95.     if (keystate[SDL_GetScancodeFromName(key)]) {
  96.        
  97.         return true;
  98.     }
  99.  
  100.     return false;
  101. }
  102. bool gMouseDown(const char* button) {
  103.     int x, y;
  104.     Uint32 buttons = SDL_GetMouseState(&x, &y);
  105.  
  106.     if (strcmp(button, "left") == 0) {
  107.         return buttons & SDL_BUTTON(SDL_BUTTON_LEFT);
  108.     }
  109.  
  110.     if (strcmp(button, "right") == 0) {
  111.         return buttons & SDL_BUTTON(SDL_BUTTON_RIGHT);
  112.     }
  113.  
  114.     return false;
  115. }
  116. int gMouseX(){
  117.     int x;
  118.     SDL_GetMouseState(&x, NULL);
  119.     return x;
  120. }
  121. int gMouseY(){
  122.     int y;
  123.     SDL_GetMouseState(NULL, &y);
  124.     return y;
  125. }
  126. bool gIsRunning(){
  127.     while (SDL_PollEvent(&event)) {
  128.         if (event.type == SDL_QUIT) return false;
  129.     }
  130.     return true;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement