Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using renderer on MacOS #5228

Closed
StarSugar opened this issue Jan 16, 2022 · 3 comments
Closed

Using renderer on MacOS #5228

StarSugar opened this issue Jan 16, 2022 · 3 comments

Comments

@StarSugar
Copy link

I have a problem, please help me! I tried to draw a graph with the renderer on my MacBook, but nothing shows up on the window, even though I've used SDL_RenderPresent.
Here is my code.

//
//  main.c
//  test
//
//  Created by *** on 2022/1/15.
//

#include <stdio.h>
#include "SDL2/SDL.h"
SDL_Texture* loadBMP(char* filePath,SDL_Renderer* renderer){
    SDL_Surface* bmp = NULL;
    SDL_Texture* texture = NULL;
    bmp = SDL_LoadBMP(filePath);
    if(bmp == NULL){
        printf("\nThe error is %s\n",SDL_GetError());
        return (SDL_Texture*)-1;
    }
    texture = SDL_CreateTextureFromSurface(renderer, bmp);
    SDL_FreeSurface(bmp);
    if(texture == NULL){
        printf("\nThe error is %s\n",SDL_GetError());
        return (SDL_Texture*)-1;
    }
    return texture;
}

void drawTextrue(int x,int y,SDL_Texture* texture,SDL_Renderer* renderer){
    SDL_Rect rectangle;
    rectangle.x = x;
    rectangle.y = y;
    SDL_QueryTexture(texture, NULL, NULL, &rectangle.x, &rectangle.y);
    SDL_RenderCopy(renderer, texture, NULL, &rectangle);
}

int main(int argc, const char * argv[]) {
    // insert code here...
    SDL_Window* window = NULL;
    SDL_Renderer* renderer = NULL;
    
    if(SDL_Init(SDL_Init(SDL_INIT_EVERYTHING)) < 0){
        printf("\nThere is a error:%s\n",SDL_GetError());
    }
    
    window = SDL_CreateWindow("test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
    if(window == NULL){
        printf("\nThe error is %s\n",SDL_GetError());
        return 1;
    }
    
    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    if(renderer == NULL){
        printf("\nThe error is %s\n",SDL_GetError());
        return 1;
    }
    
    SDL_Texture* background = NULL;
    background = loadBMP("/Users/gaoyuzhuo/Desktop/background.bmp", renderer);
    if(background == (SDL_Texture*)-1){
        printf("\n The error is %s\n",SDL_GetError());
        return 1;
    }
    
    SDL_Texture* image = NULL;
    image = loadBMP("/Users/gaoyuzhuo/Desktop/image.bmp", renderer);
    if(image == (SDL_Texture*)-1){
        printf("\n The error is %s\n",SDL_GetError());
        return 1;
    }
    
    int bx = 0;
    int by = 0;
    SDL_RenderClear(renderer);
    SDL_QueryTexture(background, NULL, NULL, &bx, &by);
    drawTextrue(0, 0, background, renderer);
    drawTextrue(0, by, background, renderer);
    drawTextrue(bx, 0, background, renderer);
    drawTextrue(bx, by, background, renderer);
    SDL_RenderPresent(renderer);
    
    drawTextrue(0, 0, image, renderer);
    SDL_RenderPresent(renderer);
    
    SDL_bool quit = SDL_FALSE;
    while(!quit){
        SDL_Event e;
        while(SDL_PollEvent(&e)){
            if(e.type == SDL_QUIT){
                quit = SDL_TRUE;
            }
        }
    }
    
    SDL_DestroyRenderer(renderer);
    SDL_DestroyTexture(image);
    SDL_DestroyTexture(background);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}

result:
avatar
I'm looking forward your reply!

@slouken
Copy link
Collaborator

slouken commented Jan 16, 2022

I haven't tried to run this, but usually the drawing is done every frame, e.g.

//
//  main.c
//  test
//
//  Created by *** on 2022/1/15.
//

#include <stdio.h>
#include "SDL2/SDL.h"
SDL_Texture* loadBMP(char* filePath,SDL_Renderer* renderer){
    SDL_Surface* bmp = NULL;
    SDL_Texture* texture = NULL;
    bmp = SDL_LoadBMP(filePath);
    if(bmp == NULL){
        printf("\nThe error is %s\n",SDL_GetError());
        return (SDL_Texture*)-1;
    }
    texture = SDL_CreateTextureFromSurface(renderer, bmp);
    SDL_FreeSurface(bmp);
    if(texture == NULL){
        printf("\nThe error is %s\n",SDL_GetError());
        return (SDL_Texture*)-1;
    }
    return texture;
}

void drawTextrue(int x,int y,SDL_Texture* texture,SDL_Renderer* renderer){
    SDL_Rect rectangle;
    rectangle.x = x;
    rectangle.y = y;
    SDL_QueryTexture(texture, NULL, NULL, &rectangle.x, &rectangle.y);
    SDL_RenderCopy(renderer, texture, NULL, &rectangle);
}

int main(int argc, const char * argv[]) {
    // insert code here...
    SDL_Window* window = NULL;
    SDL_Renderer* renderer = NULL;
    
    if(SDL_Init(SDL_Init(SDL_INIT_EVERYTHING)) < 0){
        printf("\nThere is a error:%s\n",SDL_GetError());
    }
    
    window = SDL_CreateWindow("test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
    if(window == NULL){
        printf("\nThe error is %s\n",SDL_GetError());
        return 1;
    }
    
    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    if(renderer == NULL){
        printf("\nThe error is %s\n",SDL_GetError());
        return 1;
    }
    
    SDL_Texture* background = NULL;
    background = loadBMP("/Users/gaoyuzhuo/Desktop/background.bmp", renderer);
    if(background == (SDL_Texture*)-1){
        printf("\n The error is %s\n",SDL_GetError());
        return 1;
    }
    
    SDL_Texture* image = NULL;
    image = loadBMP("/Users/gaoyuzhuo/Desktop/image.bmp", renderer);
    if(image == (SDL_Texture*)-1){
        printf("\n The error is %s\n",SDL_GetError());
        return 1;
    }
    
    SDL_bool quit = SDL_FALSE;
    while(!quit){
        SDL_Event e;
        while(SDL_PollEvent(&e)){
            if(e.type == SDL_QUIT){
                quit = SDL_TRUE;
            }
        }
    
        int bx = 0;
        int by = 0;
        SDL_RenderClear(renderer);
        SDL_QueryTexture(background, NULL, NULL, &bx, &by);
        drawTextrue(0, 0, background, renderer);
        drawTextrue(0, by, background, renderer);
        drawTextrue(bx, 0, background, renderer);
        drawTextrue(bx, by, background, renderer);
        SDL_RenderPresent(renderer);
    
        drawTextrue(0, 0, image, renderer);
        SDL_RenderPresent(renderer);
    }
    
    SDL_DestroyRenderer(renderer);
    SDL_DestroyTexture(image);
    SDL_DestroyTexture(background);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}

@1bsyl
Copy link
Contributor

1bsyl commented Jan 17, 2022

I think the drawTexture() is wrong :

  • you miss to set a w / h to rectangle.
    eg
    rectangle.w = someval;
    rectangle.h = someval;
    otherwise it scales to 0 x 0

then you may want to set
rect.x = rect.y to smaller value to draw within the window size
(i didn't try with you image so this may not be needed)

So closing ..

@1bsyl 1bsyl closed this as completed Jan 17, 2022
@1bsyl
Copy link
Contributor

1bsyl commented Jan 17, 2022

ping @StarSugar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants