#include<stdio.h>
#include<SDL2/SDL.h>
#include<SDL2/SDL_timer.h>
#define SCREEN_WIDTH 1280
#define SCREEN_HEIGHT 720
#define SPEED 250
#define FPS 60
int main(int argc,char* args[])
{
SDL_Surface *imageSurface = NULL;
SDL_Surface *windowSurface = NULL;
/*Initializing Graphics and Timer System*/
if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER)!=0){
printf("Error Initializing SDL: %s\n",SDL_GetError());
return 1;
}
/*Creating a Window*/
SDL_Window* window = NULL;
window = SDL_CreateWindow("HELI",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
SCREEN_WIDTH,SCREEN_HEIGHT,
SDL_WINDOW_RESIZABLE);
windowSurface = SDL_GetWindowSurface(window);
if(window == NULL){
printf("Error Creating Window: %s\n",SDL_GetError());
SDL_Quit();
return 1;
}
/*Creating a Renderer, Which Sets Up the Graphics Hardware*/
Uint32 render_flags = SDL_RENDERER_ACCELERATED;
SDL_Renderer* renderer = NULL;
renderer = SDL_CreateRenderer(window,-1,render_flags);
if(renderer == NULL){
printf("Error Creating Renderer: %s\n",SDL_GetError());
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
/*Loading the Player image into memory using SDL Image Library Function*/
SDL_Surface* player = NULL;
player = SDL_LoadBMP("/home/betmon69/Documents/Heli/Images/player.bmp");
if(player == NULL){
printf("Error Creating Player\n");
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
/*Loading the Player Image Data Into the Graphics Hardware's Memory*/
SDL_Texture* player_tex = NULL;
player_tex = SDL_CreateTextureFromSurface(renderer,player);
SDL_FreeSurface(player);
if(player_tex == NULL){
printf("Error Creating Player Texture: %s\n",SDL_GetError());
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
/*Structure to Hold the Position and Size of the Player*/
SDL_Rect dest;
/*Get the Dimensions of Player*/
SDL_QueryTexture(player_tex,NULL,NULL,&dest.w,&dest.h);
dest.w;
dest.h;
/* Start the Player in Center of Screen(ami nije korbo eta)*/
float x_pos = (SCREEN_WIDTH - dest.w)/2;
float y_pos = (SCREEN_HEIGHT - dest.h);
float x_vel = 0;
float y_vel = 0;
/*Keep Track of Which Inputs are given*/
int up = 0;
int down = 0;
int left = 0;
int right = 0;
/*Animation Loop*/
SDL_Event event;
imageSurface = SDL_LoadBMP("/home/betmon69/Documents/Heli/Images/background.bmp");
if(imageSurface == NULL){
printf("SDL Could Not Load Background! SDL Error: %s\n",SDL_GetError());
}
bool running = true;
while(running){
while(SDL_PollEvent(&event)){
switch(event.type){
case SDL_QUIT:
running = false;
break;
case SDL_KEYDOWN:
switch(event.key.keysym.scancode){
case SDL_SCANCODE_W:
case SDL_SCANCODE_UP:
up = 1;
break;
case SDL_SCANCODE_A:
case SDL_SCANCODE_LEFT:
left = 1;
break;
case SDL_SCANCODE_S:
case SDL_SCANCODE_DOWN:
down = 1;
break;
case SDL_SCANCODE_D:
case SDL_SCANCODE_RIGHT:
right = 1;
break;
}
break;
case SDL_KEYUP:
switch(event.key.keysym.scancode){
case SDL_SCANCODE_W:
case SDL_SCANCODE_UP:
up = 0;
break;
case SDL_SCANCODE_A:
case SDL_SCANCODE_LEFT:
left = 0;
break;
case SDL_SCANCODE_S:
case SDL_SCANCODE_DOWN:
down = 0;
break;
case SDL_SCANCODE_D:
case SDL_SCANCODE_RIGHT:
right = 0;
break;
}
break;
}
}
SDL_BlitSurface(imageSurface,NULL,windowSurface,NULL);
SDL_UpdateWindowSurface(window);
/*Determine the Velocity*/
x_vel = 0;
y_vel = 0;
if(up && !down) y_vel = -SPEED;
if(down && !up) y_vel = SPEED;
if(left && !right) x_vel = -SPEED;
if(right && !left) x_vel = SPEED;
/*Update Positions*/
x_pos += x_vel/60;
y_pos +=y_vel/60;
/*Collision Detection with Borders*/
if(x_pos<=0) x_pos = 0;
if(y_pos<=0) y_pos = 0;
if(x_pos>=SCREEN_WIDTH - dest.w) x_pos = SCREEN_WIDTH - dest.w;
if(y_pos>=SCREEN_HEIGHT - dest.h) y_pos = SCREEN_HEIGHT - dest.h;
/*Set the Positions in the Structure*/
dest.y = (int) y_pos;
dest.x = (int) x_pos;
/*Clear the Window*/
SDL_RenderClear(renderer);
/*Draw the Player to the Window*/
SDL_RenderCopy(renderer,player_tex,NULL,&dest);
SDL_RenderPresent(renderer);
SDL_Delay(1000/60);
}
/*Clean Up Resources Before Exiting*/
SDL_DestroyTexture(player_tex);
SDL_FreeSurface(imageSurface);
SDL_FreeSurface(windowSurface);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Итак, в моем коде вы можете видеть, что я создал игрока, который может свободно перемещаться, и также происходит столкновение. Я просто хочу создать фон для игрока, которого я только что создал. Но фон не создает. Я использовал SDL_Surface, а также использовал SDL_GetWindowSurface (). но я получаю ошибку «Ошибка создания рендерера: рендерер уже связан с окном». Что я должен изменить, чтобы иметь фон для моего игрока? Пожалуйста, помогите.