Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SDL.h>
- #include <SDL_keycode.h>
- #include <stdio.h>
- #include <stdbool.h>
- #include <math.h>
- #include <SDL_image.h>
- SDL_Window* window ;
- SDL_Renderer* renderer;
- SDL_Event event;
- int gInit(){
- return SDL_Init(SDL_INIT_VIDEO);
- }
- int gCreateWindow(const char* title, int w, int h){
- window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, SDL_WINDOW_SHOWN);
- renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
- if (window == NULL) {
- printf("Window error: ",SDL_GetError());
- return 1;}
- if (renderer == NULL) {
- printf("Renderer error: ",SDL_GetError());
- return 1;}
- return 0;
- }
- void gSetColor(int r, int g, int b, int a){
- SDL_SetRenderDrawColor(renderer, r, g, b, a);
- }
- void gClear(){
- SDL_RenderClear(renderer);
- }
- void gClose(){
- SDL_DestroyRenderer(renderer);
- SDL_DestroyWindow(window);
- SDL_Quit();
- }
- void gQuit(){
- SDL_Quit();
- }
- void gCreateFillRect(int x, int y, int w, int h){
- SDL_Rect rect = {x, y, w, h};
- SDL_RenderFillRect(renderer, &rect);
- }
- void gUpdate(){
- SDL_RenderPresent(renderer);
- }
- void gCreateRect(int x, int y, int w, int h){
- SDL_Rect rect = {x, y, w, h};
- SDL_RenderDrawRect(renderer, &rect);
- }
- void gCreateLine(int x1, int y1, int x2, int y2){
- SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
- }
- void gCreateCircle(int x, int y, int r){
- for (int i = 0; i < 360; i++){
- SDL_RenderDrawPoint(renderer, x + r * cos(i), y + r * sin(i));
- }
- }
- void gCreatePoint(int x, int y){
- SDL_RenderDrawPoint(renderer, x, y);
- }
- SDL_Texture* gLoadTexture(const char* path) {
- SDL_Surface* surface = IMG_Load(path);
- if (!surface) {
- printf("Unable to load image %s! SDL_image Error: %s\n", path, IMG_GetError());
- return NULL;
- }
- SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
- if (!texture) {
- printf("Unable to create texture from %s! SDL Error: %s\n", path, SDL_GetError());
- return NULL;
- }
- SDL_FreeSurface(surface);
- return texture;
- }
- void gCreatePicture(const char* path, int x, int y, int w, int h) {
- SDL_Texture* texture = gLoadTexture(path);
- if (!texture) {
- printf("Unable to load texture image on path %s!\n", path);
- return;
- }
- SDL_Rect destRect = { x, y, w, h };
- SDL_RenderCopy(renderer, texture, NULL, &destRect);
- SDL_DestroyTexture(texture);
- }
- bool gKeyPressed(const char *key) {
- const Uint8 *keystate = SDL_GetKeyboardState(NULL);
- //printf("Stisknuta klávesa: %s\n", key);
- if (keystate[SDL_GetScancodeFromName(key)]) {
- return true;
- }
- return false;
- }
- bool gMouseDown(const char* button) {
- int x, y;
- Uint32 buttons = SDL_GetMouseState(&x, &y);
- if (strcmp(button, "left") == 0) {
- return buttons & SDL_BUTTON(SDL_BUTTON_LEFT);
- }
- if (strcmp(button, "right") == 0) {
- return buttons & SDL_BUTTON(SDL_BUTTON_RIGHT);
- }
- return false;
- }
- int gMouseX(){
- int x;
- SDL_GetMouseState(&x, NULL);
- return x;
- }
- int gMouseY(){
- int y;
- SDL_GetMouseState(NULL, &y);
- return y;
- }
- bool gIsRunning(){
- while (SDL_PollEvent(&event)) {
- if (event.type == SDL_QUIT) return false;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement