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
Comments
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;
} |
I think the drawTexture() is wrong :
then you may want to set So closing .. |
ping @StarSugar |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
result:
I'm looking forward your reply!
The text was updated successfully, but these errors were encountered: