用SDL写了一个动画,帧率到后面只有5,6帧什么情况
c4droid吧
全部回复
仅看楼主
level 8
strongrex2001 楼主
如题
2016年10月29日 09点10分 1
level 13
处理动画时间变长,挤压了刷新时间!
2016年10月29日 09点10分 2
有什么解决方法吗w
2016年10月29日 09点10分
需要绘制约200个圆怎么办
2016年10月29日 09点10分
用批量画圆
2016年10月29日 09点10分
@savage200 能说说怎么实现么w
2016年10月29日 09点10分
level 8
strongrex2001 楼主
大家给点实现方法呗
2016年10月29日 10点10分 3
level 12
是不是有内存申请的操作?
2016年10月29日 10点10分 4
vector push_back算吗
2016年10月29日 10点10分
@strongrex2001 不算、但是如果你在循环里用了这个数组会有些影响
2016年10月29日 10点10分
2016年10月29日 10点10分
@lxfly2000 我把所有用vector储存最后统一渲染的地方都改成了直接BiltTexture稍微好点,但帧率后面还是不到10怎么办啊
2016年10月29日 11点10分
level 8
strongrex2001 楼主
用timer进行不绘制的update有影响吗
2016年10月29日 11点10分 5
level 8
strongrex2001 楼主
主程序 game.cpp
————————
#include "SDL2/SDL.h"
#include "projectile.h"
#include "DrawHelper.h"
#include "structures.h"
#include "boss_0.h"
#include "SDL2/SDL_image.h"
#include "SDL2/SDL_timer.h"
#include "SDL2/SDL_ttf.h"
#include <vector>
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
static int frame=0;
const int maxProj=224;
const string Fonts = "/system/fonts/DroidSansFallback.ttf";
extern int WIDTH=480,HEIGHT=800;
extern Projectile * proj= new Projectile[maxProj];
extern vector<SDL_Texture*> drawTex = vector<SDL_Texture*>();
extern vector<Point> drawPos = vector<Point>();
extern SDL_Window* win=NULL;
extern SDL_Surface* screen=NULL;
extern SDL_Renderer* renderer=NULL;
extern boss_0 boss=boss_0();
Uint32 Update(Uint32 interval, void *param);
int main(int argc, char** argv)
{
//Initialize
int quit=0;
SDL_Init(SDL_INIT_EVERYTHING);
TTF_Init();
TTF_Font * font=NULL;
font=TTF_OpenFont(Fonts.c_str(),18);
SDL_Color fontColor={255,0,0,255};
SDL_Surface* text=NULL;
win=SDL_CreateWindow("Game",0,0,WIDTH,HEIGHT,0);
SDL_GetWindowSize(win,&WIDTH,&HEIGHT);
renderer=SDL_CreateRenderer(win,-1,SDL_RENDERER_ACCELERATED);
SDL_Event Event;
boss=boss_0();
Uint32 rmask, gmask, bmask, amask;
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
screen = SDL_CreateRGBSurface(0,WIDTH,HEIGHT,32,rmask, gmask, bmask, amask);
//Update
Uint32 delay = 20; /* To round it down to the nearest 10 ms */
void * my_callback_param;
SDL_TimerID my_timer_id = SDL_AddTimer(delay, Update, my_callback_param);
//Draw
while(!quit)
{
char frm[10];
sprintf(frm,"%d",frame);text=TTF_RenderUTF8_Blended(font,frm,fontColor);
drawTex.empty();
drawPos.empty();
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 80, 80, 80));
/*Point testpoint(10,10);
circle::draw(testpoint,127,200,200,255,screen);*/
drawTex.push_back(SDL_CreateTextureFromSurface(renderer,screen));
drawPos.push_back(Point(0,0));
drawTex.push_back(SDL_CreateTextureFromSurface(renderer,text));
drawPos.push_back(Point(0,0));
boss.Draw();
SDL_RenderClear(renderer);
for(int i=0;i<drawTex.size();i++)
DrawHelper::BiltTexture(drawPos[i].x,drawPos[i].y,drawTex[i],renderer);
for(int i=0;i<maxProj;i++)
{
proj[i].Draw();
}
SDL_RenderPresent(renderer);
while (SDL_PollEvent(&Event))
{
if (Event.type==SDL_FINGERUP)
{
quit=1;break;
}
}
}
//Quit
SDL_Delay(100);
SDL_Quit();
return 0;
}
Uint32 Update(Uint32 interval, void *param)
{
/*SDL_Event event;
SDL_UserEvent userevent;
In this example, our callback pushes an SDL_USEREVENT event
into the queue, and causes our callback to be called again at the
same interval:
userevent.type = SDL_USEREVENT;
userevent.code = 0;
userevent.data1 = NULL;
userevent.data2 = NULL;
event.type = SDL_USEREVENT;
event.user = userevent;
SDL_PushEvent(&event);*/
for(int i=0;i<maxProj;i++)
proj[i].Update();
boss.Update();
frame++;
return(interval);
}
2016年10月29日 11点10分 7
level 8
strongrex2001 楼主
头文件boss_0.h
————————
#ifndef BOSS_0_H
#define BOSS_0_H
#include "structures.h"
#include "DrawHelper.h"
#include "projectile.h"
#include "SDL2/SDL_image.h"
#include <vector>
using namespace std;
extern int WIDTH,HEIGHT;
extern vector<SDL_Texture*> drawTex;
extern Projectile * proj;
extern vector<Point> drawPos;
extern SDL_Renderer* renderer;
extern const int maxProj;
class boss_0
{
private:
int timer;
SDL_Texture *pic;
SDL_Surface *picSur;
public:
Point position;
int DrawID;
boss_0()
{
timer=0;
picSur = IMG_Load("assets/boss_0.png");
pic = SDL_CreateTextureFromSurface(renderer, picSur);position=Point(WIDTH/2-25,HEIGHT/4);
}
void Draw()
{
drawTex.push_back(pic);
drawPos.push_back(position);
//DrawID=drawTex.size()-1;
}
void Update()
{
timer++;
if (timer==70)
{
timer=0;
}
if (timer%5 == 0)
for (int j=0;j<maxProj;j++)
{
if(!proj[j].active)
{
proj[j]=Projectile(position,Point((timer-37.5)/12.0,timer/33.0));
break;
}
}
}
};
#endif
2016年10月29日 11点10分 8
level 8
strongrex2001 楼主
头文件DrawHelper.h
#ifndef DRAW_HELPER_H
#define DRAW_HELPER_H
#include "SDL2/SDL.h"
class DrawHelper
{
public:
static void setpixel(SDL_Surface *screen, int x, int y, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
{
Uint32 *pixmem32;
Uint32 colour;
colour = SDL_MapRGBA( screen->format, r, g, b, a );
pixmem32 = (Uint32*) screen->pixels + y*screen->w+ x;
*pixmem32 = colour;
}
static void BiltTexture(int x,int y, SDL_Texture * Draw_Texture, SDL_Renderer * Draw_Render)
{
SDL_Rect Draw_Render_Rect;
Draw_Render_Rect.x = x;
Draw_Render_Rect.y = y;
int w = 0, h = 0;
SDL_QueryTexture(Draw_Texture, NULL, NULL, &w, &h);
Draw_Render_Rect.w = w;
Draw_Render_Rect.h = h;
SDL_RenderCopy(Draw_Render, Draw_Texture, NULL, &Draw_Render_Rect);
}
};
#endif
2016年10月29日 11点10分 9
level 8
strongrex2001 楼主
头文件projectile.h
#ifndef PROJECTILE_H
#define PROJECTILE_H
#include "structures.h"
#include "DrawHelper.h"
#include "SDL2/SDL.h"
extern int WIDTH,HEIGHT;
extern SDL_Surface* screen;
extern SDL_Renderer* renderer;
class Projectile
{
public:
bool active;
int DrawID;
Point velocity;
Point position;
Projectile(){
active=false;
position = Point();
velocity = Point();
}
Projectile(Point position_,Point velocity_){
active=true;
position=position_;
velocity=velocity_;
}
void Update()
{
if(active)
{
position=position+velocity;
if(position.x>=WIDTH||position.y>=HEIGHT||position.x<0||position.y<0)
active=false;
}
}
void Draw()
{
if(active)
circle::draw(position,renderer);
else return;
}
};
#endif
2016年10月29日 11点10分 10
level 8
strongrex2001 楼主
头文件structures.h
#ifndef STRUCTS_H
#define STRUCTS_H
#include "SDL2/SDL.h"
#include "DrawHelper.h"
extern int WIDTH,HEIGHT;
static SDL_Texture *pic;
static SDL_Surface *picSur;
class Point
{
public:
float x,y;
Point(float _x,float _y){
x=_x;y=_y;
}
Point(){
x=y=0;
}
Point operator=(Point toCopy)
{
x=toCopy.x;
y=toCopy.y;
return *this;
}
Point operator+(Point toAdd)
{
Point tmp;
tmp.x=x+toAdd.x;
tmp.y=y+toAdd.y;
return tmp;
}
Point operator-(Point toMinus)
{
Point tmp;
tmp.x=x-toMinus.x;
tmp.y=y-toMinus.y;
return tmp;
}
};
const int map_circle10[10][10]={
{0,0,85,255,255,255,255,85,0,0},
{0,170,255,255,255,255,255,255,170,0},
{85,255,255,255,255,255,255,255,255,85},
{255,255,255,255,255,255,255,255,255,255},
{255,255,255,255,255,255,255,255,255,255},
{255,255,255,255,255,255,255,255,255,255},
{255,255,255,255,255,255,255,255,255,255},
{85,255,255,255,255,255,255,255,255,85},
{0,170,255,255,255,255,255,255,170,0},
{0,0,85,255,255,255,255,85,0,0}
};
class circle
{
public:
static void draw(Point position,Uint8 r, Uint8 g, Uint8 b, Uint8 a, SDL_Surface* screen)
{
for(int dx=position.x;dx<position.x+10;dx++)
for(int dy=position.y;dy<position.y+10;dy++)
DrawHelper::setpixel(screen,dx,dy,r,g,b,map_circle10[dx][dy]);
}
static void draw(Point position, SDL_Renderer* renderer)
{
if(!picSur)
{
picSur=SDL_LoadBMP("assets/proj0.bmp");
pic = SDL_CreateTextureFromSurface(renderer, picSur);
}
DrawHelper::BiltTexture(position.x,position.y,pic,renderer);
}
};
#endif
2016年10月29日 11点10分 11
level 8
strongrex2001 楼主
如果是在C#里,相同功能的代码50帧妥妥的,但是C4Droid上到后面就不行了
2016年10月29日 11点10分 12
level 8
strongrex2001 楼主
看别人都是讲怎么怎么限制帧率,我感到很无语&*#%……
2016年10月29日 11点10分 13
level 8
strongrex2001 楼主
我好想知道为什么了
Vector::empty是判断,不是清空!
2016年10月29日 12点10分 14
1