저는 C + + 학습의 최종 목표로 SDL2를보다 재미 있고 상호 작용 방식으로 사용하는 방법을 가르치는 this tutorial을 따르고 있습니다.SDL2 렌더러가 문제를 일으키고 있습니다.
이렇게하려면 선, 다각형 및 원을 그릴 수 있어야합니다.
그래서 화면에 창을 만드는 방법을 설명하는 파트 1과 이벤트 처리를 소개하는 파트 3을 읽은 후 렌더러를 만드는 방법과 드로잉을 만드는 방법을 각각 설명하는 torward 파트 7과 8로 향했다. 화면에 직사각형. 이것은 지금까지 가지고있는 코드입니다. 튜토리얼의 코드와 정확히 같지 않습니다. SDL 객체를 전달하는 구조체를 도입하고 혼동 스러웠던 모든 오류 처리를 제거했습니다.
#include <SDL2/SDL.h>
//screen dimensions costants
#define SCREEN_WIDTH 540
#define SCREEN_HEIGHT 960
//data structure holding the objects needed to create a window and draw on it
struct interface {
SDL_Window * window = NULL;
SDL_Surface * surface = NULL;
SDL_Renderer * renderer = NULL;
};
//function which inits the sdl and creates an interface object
interface init() {
interface screen;
SDL_Init(SDL_INIT_VIDEO);
screen.window = SDL_CreateWindow("", 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
screen.surface = SDL_GetWindowSurface(screen.window);
screen.renderer = SDL_CreateRenderer(screen.window, -1, SDL_RENDERER_ACCELERATED);
return screen;
}
//function to free the memory and close the sdl application
void close(interface screen) {
SDL_DestroyRenderer(screen.renderer);
SDL_DestroyWindow(screen.window);
screen.renderer = NULL;
screen.window = NULL;
SDL_Quit();
}
int main(int argc, char* args[]) {
//start the application
interface screen = init();
//setup for event handling
bool quit = false;
SDL_Event event;
//the shape to render
SDL_Rect fillRect = { SCREEN_WIDTH/4, SCREEN_HEIGHT/4, SCREEN_WIDTH/2, SCREEN_HEIGHT/2 };
//main loop which first handles events
while (!quit) {
while (SDL_PollEvent(&event) != 0) {
if (event.type == SDL_QUIT)
quit = true;
}
//should draw a red rectangle on the screen
SDL_SetRenderDrawColor(screen.renderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(screen.renderer);
SDL_SetRenderDrawColor(screen.renderer, 0xFF, 0x00, 0x00, 0xFF);
SDL_RenderFillRect(screen.renderer, &fillRect);
}
//End the application
close(screen);
return 0;
}
문제는 화면에 아무 것도 그려지지 않으며 (검은 색으로 유지됨), 줄을 지우면 screen.surface = SDL_GetWindowSurface(screen.window);
또한 응용 프로그램을 종료하기가 어려워 보이는 방식으로 많이 뒤떨어지기 시작합니다 .
저는 C4droid와 C4droid 용 SDL 플러그인을 사용하여 안드로이드에서 프로그래밍하고 있습니다.
왜 그런 일이 발생합니까? 내가 도대체 뭘 잘못하고있는 겁니까?
EDIT 문제 end
에 close
이름을 변경하여 상기 메인 루프의 끝에 SDL_RenderPresent(screen.renderer);
호출을 포함함으로써 해결했다. 이 설정으로 화면 표면 을 삭제해야합니다. 또는 프로그램에서 아무 것도 그려지지 않습니다. 내가 가지고 있지 않는 한 오히려 안드로이드보다 내가 윈도우 (MSYS2)의 코드를 테스트 한 모든의
'SDL_RenderPresent'는 어디에 있습니까? 게다가, nonstatic 함수'close'를 호출하는 것은 시스템 기능과 별칭이되는 것처럼 정말 나쁜 생각입니다. – keltar
시스템 기능? 'close' 시스템 기능이 있습니까? – user6245072
편집 : 결국 표면이 필요한 것처럼 보입니다. 'SDL_RenderPresent' 호출은 렌더러로 렌더링 된 것이 무엇이든 나타나게하기 위해 모든 루프 (마지막에 가능)에 필요합니다. –