[英]SDL2 C++ - Multiple Image Rendering Bug
在開始之前,我使用的IDE是Code :: Blocks。
我將在另一個與我的學習項目不同的項目中練習在線學習有關SDL的知識。 我想在背景頂部加載一個球的圖像。 在我的研究項目中效果很好。 我將代碼復制到測試項目中,然后球消失了。 僅背景可見。
我刪除了涉及背景的代碼,並且效果很好。
我的代碼:
#include <iostream>
#include <cstdlib>
#include <SDL.h>
#include <SDL_image.h>
#undef main
using namespace std;
SDL_Texture *LoadTexture (string filepath, SDL_Renderer *renderTarget){
SDL_Texture *texture = 0;
SDL_Surface *surface = IMG_Load(filepath.c_str());
texture = SDL_CreateTextureFromSurface(renderTarget, surface);
SDL_FreeSurface(surface);
return texture;
}
int main () {
SDL_Window *window = 0;
SDL_Texture *ball = 0;
SDL_Texture *background_sky = 0;
SDL_Renderer *renderTarget = 0;
int frameW, frameH;
int textureW, textureH;
//--Crop Instructions--//
frameW = textureW / 1; //row division
frameH = textureH / 1; //column division
SDL_Rect ballcrop;
ballcrop.x = ballcrop.y = 0;
ballcrop.w = frameW; //length of selected area
ballcrop.h = frameH; //height of selected area
SDL_Rect ballpos;
ballpos.x = ballpos.y = 10; //ball position
ballpos.w = ballpos.h = 64; //size of crop
//---------------------//
SDL_Init(SDL_INIT_EVERYTHING);
IMG_Init(IMG_INIT_PNG);
window = SDL_CreateWindow("Ball", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 500, 500, SDL_WINDOW_SHOWN);
renderTarget = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
ball = LoadTexture("ball.png", renderTarget);
background_sky = LoadTexture("bg_sky.png", renderTarget);
SDL_QueryTexture(ball, NULL, NULL, &textureW, &textureH); //crop action
bool Active = true;
SDL_Event ev;
while (Active) {
while (SDL_PollEvent(&ev)!=0) {
if (ev.type == SDL_QUIT)
Active = false;
else if (ev.type == SDL_KEYDOWN) {
switch (ev.key.keysym.sym) {
case SDLK_UP:
ballpos.y -= 5;
break;
case SDLK_DOWN:
ballpos.y += 5;
break;
}
}
}
SDL_RenderClear(renderTarget);
SDL_RenderCopy(renderTarget, background_sky, NULL, NULL);
SDL_RenderCopy(renderTarget, ball, &ballcrop, &ballpos);
SDL_RenderPresent(renderTarget);
}
SDL_DestroyWindow(window);
SDL_DestroyTexture(background_sky);
SDL_DestroyTexture(ball);
SDL_DestroyRenderer(renderTarget);
window = 0;
ball = background_sky = 0;
renderTarget = 0;
IMG_Quit();
SDL_Quit();
return 0;
}
我認為我不想顯示我的研究項目的源代碼,因為它的內容極其混亂。
任何幫助,將不勝感激 :)
計算frameW
和frameH
; 計算的代碼
//--Crop Instructions--//
frameW = textureW / 1; //row division
frameH = textureH / 1; //column division
在初始化之前使用textureW
和textureH
。 移動整個“裁剪指令”塊,直到調用SDL_QueryTexture
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.