Update the code for the iOS demos to handle modern devices. Fixes bug #3337
This commit is contained in:
parent
d3410d304c
commit
448940e133
12 changed files with 233 additions and 143 deletions
|
@ -8,7 +8,6 @@
|
|||
#include "math.h"
|
||||
#include "common.h"
|
||||
|
||||
#define MILLESECONDS_PER_FRAME 16 /* about 60 frames per second */
|
||||
#define DAMPING 0.5f; /* after bouncing off a wall, damping coefficient determines final speed */
|
||||
#define FRICTION 0.0008f /* coefficient of acceleration that opposes direction of motion */
|
||||
#define GRAVITY_CONSTANT 0.004f /* how sensitive the ship is to the accelerometer */
|
||||
|
@ -31,9 +30,9 @@ static SDL_Texture *ship = 0; /* texture for spaceship */
|
|||
static SDL_Texture *space = 0; /* texture for space (background */
|
||||
|
||||
void
|
||||
render(SDL_Renderer *renderer, int w, int h)
|
||||
render(SDL_Renderer *renderer, int w, int h, double deltaTime)
|
||||
{
|
||||
|
||||
double deltaMilliseconds = deltaTime * 1000;
|
||||
float speed;
|
||||
|
||||
/* get joystick (accelerometer) axis values and normalize them */
|
||||
|
@ -54,10 +53,10 @@ render(SDL_Renderer *renderer, int w, int h)
|
|||
*/
|
||||
shipData.vx +=
|
||||
ax * SDL_IPHONE_MAX_GFORCE / SINT16_MAX * GRAVITY_CONSTANT *
|
||||
MILLESECONDS_PER_FRAME;
|
||||
deltaMilliseconds;
|
||||
shipData.vy +=
|
||||
ay * SDL_IPHONE_MAX_GFORCE / SINT16_MAX * GRAVITY_CONSTANT *
|
||||
MILLESECONDS_PER_FRAME;
|
||||
deltaMilliseconds;
|
||||
|
||||
speed = sqrt(shipData.vx * shipData.vx + shipData.vy * shipData.vy);
|
||||
|
||||
|
@ -67,10 +66,10 @@ render(SDL_Renderer *renderer, int w, int h)
|
|||
float diry = shipData.vy / speed; /* normalized y velocity */
|
||||
|
||||
/* update velocity due to friction */
|
||||
if (speed - FRICTION * MILLESECONDS_PER_FRAME > 0) {
|
||||
if (speed - FRICTION * deltaMilliseconds > 0) {
|
||||
/* apply friction */
|
||||
shipData.vx -= dirx * FRICTION * MILLESECONDS_PER_FRAME;
|
||||
shipData.vy -= diry * FRICTION * MILLESECONDS_PER_FRAME;
|
||||
shipData.vx -= dirx * FRICTION * deltaMilliseconds;
|
||||
shipData.vy -= diry * FRICTION * deltaMilliseconds;
|
||||
} else {
|
||||
/* applying friction would MORE than stop the ship, so just stop the ship */
|
||||
shipData.vx = 0.0f;
|
||||
|
@ -79,8 +78,8 @@ render(SDL_Renderer *renderer, int w, int h)
|
|||
}
|
||||
|
||||
/* update ship location */
|
||||
shipData.x += shipData.vx * MILLESECONDS_PER_FRAME;
|
||||
shipData.y += shipData.vy * MILLESECONDS_PER_FRAME;
|
||||
shipData.x += shipData.vx * deltaMilliseconds;
|
||||
shipData.y += shipData.vy * deltaMilliseconds;
|
||||
|
||||
if (shipData.x > maxx) {
|
||||
shipData.x = maxx;
|
||||
|
@ -161,9 +160,6 @@ main(int argc, char *argv[])
|
|||
|
||||
SDL_Window *window; /* main window */
|
||||
SDL_Renderer *renderer;
|
||||
Uint32 startFrame; /* time frame began to process */
|
||||
Uint32 endFrame; /* time frame ended processing */
|
||||
Sint32 delay; /* time to pause waiting to draw next frame */
|
||||
int done; /* should we clean up and exit? */
|
||||
int w, h;
|
||||
|
||||
|
@ -173,12 +169,11 @@ main(int argc, char *argv[])
|
|||
}
|
||||
|
||||
/* create main window and renderer */
|
||||
window = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
|
||||
SDL_WINDOW_OPENGL |
|
||||
SDL_WINDOW_FULLSCREEN);
|
||||
window = SDL_CreateWindow(NULL, 0, 0, 320, 480, SDL_WINDOW_FULLSCREEN | SDL_WINDOW_ALLOW_HIGHDPI);
|
||||
renderer = SDL_CreateRenderer(window, 0, 0);
|
||||
|
||||
SDL_GetWindowSize(window, &w, &h);
|
||||
SDL_RenderSetLogicalSize(renderer, w, h);
|
||||
|
||||
/* print out some info about joysticks and try to open accelerometer for use */
|
||||
printf("There are %d joysticks available\n", SDL_NumJoysticks());
|
||||
|
@ -208,24 +203,15 @@ main(int argc, char *argv[])
|
|||
done = 0;
|
||||
/* enter main loop */
|
||||
while (!done) {
|
||||
double deltaTime = updateDeltaTime();
|
||||
SDL_Event event;
|
||||
startFrame = SDL_GetTicks();
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (event.type == SDL_QUIT) {
|
||||
done = 1;
|
||||
}
|
||||
}
|
||||
render(renderer, w, h);
|
||||
endFrame = SDL_GetTicks();
|
||||
|
||||
/* figure out how much time we have left, and then sleep */
|
||||
delay = MILLESECONDS_PER_FRAME - (endFrame - startFrame);
|
||||
if (delay < 0) {
|
||||
delay = 0;
|
||||
} else if (delay > MILLESECONDS_PER_FRAME) {
|
||||
delay = MILLESECONDS_PER_FRAME;
|
||||
}
|
||||
SDL_Delay(delay);
|
||||
render(renderer, w, h, deltaTime);
|
||||
SDL_Delay(1);
|
||||
}
|
||||
|
||||
/* delete textures */
|
||||
|
|
|
@ -32,5 +32,25 @@ void
|
|||
fatalError(const char *string)
|
||||
{
|
||||
printf("%s: %s\n", string, SDL_GetError());
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, string, SDL_GetError(), NULL);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
static Uint64 prevTime = 0;
|
||||
|
||||
double
|
||||
updateDeltaTime()
|
||||
{
|
||||
Uint64 curTime;
|
||||
double deltaTime;
|
||||
|
||||
if (prevTime == 0) {
|
||||
prevTime = SDL_GetPerformanceCounter();
|
||||
}
|
||||
|
||||
curTime = SDL_GetPerformanceCounter();
|
||||
deltaTime = (double) (curTime - prevTime) / (double) SDL_GetPerformanceFrequency();
|
||||
prevTime = curTime;
|
||||
|
||||
return deltaTime;
|
||||
}
|
||||
|
|
|
@ -4,14 +4,7 @@
|
|||
* use however you want
|
||||
*/
|
||||
|
||||
#if __TVOS__
|
||||
#define SCREEN_WIDTH 1920
|
||||
#define SCREEN_HEIGHT 1080
|
||||
#else
|
||||
#define SCREEN_WIDTH 320
|
||||
#define SCREEN_HEIGHT 480
|
||||
#endif
|
||||
|
||||
extern int randomInt(int min, int max);
|
||||
extern float randomFloat(float min, float max);
|
||||
extern void fatalError(const char *string);
|
||||
extern double updateDeltaTime();
|
||||
|
|
|
@ -10,13 +10,13 @@
|
|||
#include <math.h>
|
||||
#include <time.h>
|
||||
|
||||
#define MILLESECONDS_PER_FRAME 16 /* about 60 frames per second */
|
||||
#define ACCEL 0.0001f /* acceleration due to gravity, units in pixels per millesecond squared */
|
||||
#define WIND_RESISTANCE 0.00005f /* acceleration per unit velocity due to wind resistance */
|
||||
#define MAX_PARTICLES 2000 /* maximum number of particles displayed at once */
|
||||
|
||||
static GLuint particleTextureID; /* OpenGL particle texture id */
|
||||
static SDL_bool pointSizeExtensionSupported; /* is GL_OES_point_size_array supported ? */
|
||||
static float pointSizeScale;
|
||||
/*
|
||||
used to describe what type of particle a given struct particle is.
|
||||
emitter - this particle flies up, shooting off trail particles, then finally explodes into dust particles.
|
||||
|
@ -55,7 +55,7 @@ void initializeParticles(void);
|
|||
void initializeTexture();
|
||||
int nextPowerOfTwo(int x);
|
||||
void drawParticles();
|
||||
void stepParticles(void);
|
||||
void stepParticles(double deltaTime);
|
||||
|
||||
/* helper function (used in texture loading)
|
||||
returns next power of two greater than or equal to x
|
||||
|
@ -74,8 +74,9 @@ nextPowerOfTwo(int x)
|
|||
steps each active particle by timestep MILLESECONDS_PER_FRAME
|
||||
*/
|
||||
void
|
||||
stepParticles(void)
|
||||
stepParticles(double deltaTime)
|
||||
{
|
||||
float deltaMilliseconds = deltaTime * 1000;
|
||||
int i;
|
||||
struct particle *slot = particles;
|
||||
struct particle *curr = particles;
|
||||
|
@ -93,10 +94,10 @@ stepParticles(void)
|
|||
curr->isActive = 0;
|
||||
|
||||
/* step velocity, then step position */
|
||||
curr->yvel += ACCEL * MILLESECONDS_PER_FRAME;
|
||||
curr->yvel += ACCEL * deltaMilliseconds;
|
||||
curr->xvel += 0.0f;
|
||||
curr->y += curr->yvel * MILLESECONDS_PER_FRAME;
|
||||
curr->x += curr->xvel * MILLESECONDS_PER_FRAME;
|
||||
curr->y += curr->yvel * deltaMilliseconds;
|
||||
curr->x += curr->xvel * deltaMilliseconds;
|
||||
|
||||
/* particle behavior */
|
||||
if (curr->type == emitter) {
|
||||
|
@ -111,29 +112,29 @@ stepParticles(void)
|
|||
sqrt(curr->xvel * curr->xvel + curr->yvel * curr->yvel);
|
||||
/* if wind resistance is not powerful enough to stop us completely,
|
||||
then apply winde resistance, otherwise just stop us completely */
|
||||
if (WIND_RESISTANCE * MILLESECONDS_PER_FRAME < speed) {
|
||||
if (WIND_RESISTANCE * deltaMilliseconds < speed) {
|
||||
float normx = curr->xvel / speed;
|
||||
float normy = curr->yvel / speed;
|
||||
curr->xvel -=
|
||||
normx * WIND_RESISTANCE * MILLESECONDS_PER_FRAME;
|
||||
normx * WIND_RESISTANCE * deltaMilliseconds;
|
||||
curr->yvel -=
|
||||
normy * WIND_RESISTANCE * MILLESECONDS_PER_FRAME;
|
||||
normy * WIND_RESISTANCE * deltaMilliseconds;
|
||||
} else {
|
||||
curr->xvel = curr->yvel = 0; /* stop particle */
|
||||
}
|
||||
|
||||
if (curr->color[3] <= MILLESECONDS_PER_FRAME * 0.1275f) {
|
||||
if (curr->color[3] <= deltaMilliseconds * 0.1275f) {
|
||||
/* if this next step will cause us to fade out completely
|
||||
then just mark for deletion */
|
||||
curr->isActive = 0;
|
||||
} else {
|
||||
/* otherwise, let's fade a bit more */
|
||||
curr->color[3] -= MILLESECONDS_PER_FRAME * 0.1275f;
|
||||
curr->color[3] -= deltaMilliseconds * 0.1275f;
|
||||
}
|
||||
|
||||
/* if we're a dust particle, shrink our size */
|
||||
if (curr->type == dust)
|
||||
curr->size -= MILLESECONDS_PER_FRAME * 0.010f;
|
||||
curr->size -= deltaMilliseconds * 0.010f;
|
||||
|
||||
}
|
||||
|
||||
|
@ -147,7 +148,7 @@ stepParticles(void)
|
|||
/* the number of active particles is computed as the difference between
|
||||
old number of active particles, where slot points, and the
|
||||
new size of the array, where particles points */
|
||||
num_active_particles = slot - particles;
|
||||
num_active_particles = (int) (slot - particles);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -206,7 +207,7 @@ explodeEmitter(struct particle *emitter)
|
|||
p->y = emitter->y + emitter->yvel;
|
||||
p->isActive = 1;
|
||||
p->type = dust;
|
||||
p->size = 15;
|
||||
p->size = 15 * pointSizeScale;
|
||||
/* inherit emitter's color */
|
||||
p->color[0] = emitter->color[0];
|
||||
p->color[1] = emitter->color[1];
|
||||
|
@ -244,7 +245,7 @@ spawnTrailFromEmitter(struct particle *emitter)
|
|||
p->color[3] = (0.7f) * 255;
|
||||
|
||||
/* set other attributes */
|
||||
p->size = 10;
|
||||
p->size = 10 * pointSizeScale;
|
||||
p->type = trail;
|
||||
p->isActive = 1;
|
||||
|
||||
|
@ -298,7 +299,7 @@ spawnEmitterParticle(GLfloat x, GLfloat y)
|
|||
p->xvel = 0;
|
||||
p->yvel = -sqrt(2 * ACCEL * (screen_h - y));
|
||||
/* set other attributes */
|
||||
p->size = 10;
|
||||
p->size = 10 * pointSizeScale;
|
||||
p->type = emitter;
|
||||
p->isActive = 1;
|
||||
/* our array has expanded at the end */
|
||||
|
@ -363,7 +364,7 @@ main(int argc, char *argv[])
|
|||
{
|
||||
SDL_Window *window; /* main window */
|
||||
SDL_GLContext context;
|
||||
int w, h;
|
||||
int drawableW, drawableH;
|
||||
Uint32 startFrame; /* time frame began to process */
|
||||
Uint32 endFrame; /* time frame ended processing */
|
||||
Uint32 delay; /* time to pause waiting to draw next frame */
|
||||
|
@ -391,11 +392,19 @@ main(int argc, char *argv[])
|
|||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
|
||||
|
||||
/* create main window and renderer */
|
||||
window = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
|
||||
SDL_WINDOW_OPENGL |
|
||||
SDL_WINDOW_BORDERLESS);
|
||||
window = SDL_CreateWindow(NULL, 0, 0, 320, 480,
|
||||
SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS | SDL_WINDOW_ALLOW_HIGHDPI);
|
||||
context = SDL_GL_CreateContext(window);
|
||||
|
||||
/* The window size and drawable size may be different when highdpi is enabled,
|
||||
* due to the increased pixel density of the drawable. */
|
||||
SDL_GetWindowSize(window, &screen_w, &screen_h);
|
||||
SDL_GL_GetDrawableSize(window, &drawableW, &drawableH);
|
||||
|
||||
/* In OpenGL, point sizes are always in pixels. We don't want them looking
|
||||
* tiny on a retina screen. */
|
||||
pointSizeScale = (float) drawableH / (float) screen_h;
|
||||
|
||||
/* load the particle texture */
|
||||
initializeTexture();
|
||||
|
||||
|
@ -412,8 +421,7 @@ main(int argc, char *argv[])
|
|||
glMatrixMode(GL_MODELVIEW);
|
||||
glLoadIdentity();
|
||||
|
||||
SDL_GetWindowSize(window, &screen_w, &screen_h);
|
||||
glViewport(0, 0, screen_w, screen_h);
|
||||
glViewport(0, 0, drawableW, drawableH);
|
||||
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
|
@ -436,14 +444,14 @@ main(int argc, char *argv[])
|
|||
glEnableClientState(GL_POINT_SIZE_ARRAY_OES);
|
||||
} else {
|
||||
/* if extension not available then all particles have size 10 */
|
||||
glPointSize(10);
|
||||
glPointSize(10 * pointSizeScale);
|
||||
}
|
||||
|
||||
done = 0;
|
||||
/* enter main loop */
|
||||
while (!done) {
|
||||
startFrame = SDL_GetTicks();
|
||||
SDL_Event event;
|
||||
double deltaTime = updateDeltaTime();
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (event.type == SDL_QUIT) {
|
||||
done = 1;
|
||||
|
@ -454,19 +462,10 @@ main(int argc, char *argv[])
|
|||
spawnEmitterParticle(x, y);
|
||||
}
|
||||
}
|
||||
stepParticles();
|
||||
stepParticles(deltaTime);
|
||||
drawParticles();
|
||||
SDL_GL_SwapWindow(window);
|
||||
endFrame = SDL_GetTicks();
|
||||
|
||||
/* figure out how much time we have left, and then sleep */
|
||||
delay = MILLESECONDS_PER_FRAME - (endFrame - startFrame);
|
||||
if (delay > MILLESECONDS_PER_FRAME) {
|
||||
delay = MILLESECONDS_PER_FRAME;
|
||||
}
|
||||
if (delay > 0) {
|
||||
SDL_Delay(delay);
|
||||
}
|
||||
SDL_Delay(1);
|
||||
}
|
||||
|
||||
/* delete textures */
|
||||
|
|
|
@ -8,8 +8,7 @@
|
|||
#include "common.h"
|
||||
|
||||
#define NUM_HAPPY_FACES 100 /* number of faces to draw */
|
||||
#define MILLESECONDS_PER_FRAME 16 /* about 60 frames per second */
|
||||
#define HAPPY_FACE_SIZE 32 /* width and height of happyface (pixels) */
|
||||
#define HAPPY_FACE_SIZE 32 /* width and height of happyface */
|
||||
|
||||
static SDL_Texture *texture = 0; /* reference to texture holding happyface */
|
||||
|
||||
|
@ -24,30 +23,37 @@ static struct
|
|||
units of velocity are pixels per millesecond
|
||||
*/
|
||||
void
|
||||
initializeHappyFaces()
|
||||
initializeHappyFaces(SDL_Renderer *renderer)
|
||||
{
|
||||
int i;
|
||||
int w;
|
||||
int h;
|
||||
SDL_RenderGetLogicalSize(renderer, &w, &h);
|
||||
|
||||
for (i = 0; i < NUM_HAPPY_FACES; i++) {
|
||||
faces[i].x = randomFloat(0.0f, SCREEN_WIDTH - HAPPY_FACE_SIZE);
|
||||
faces[i].y = randomFloat(0.0f, SCREEN_HEIGHT - HAPPY_FACE_SIZE);
|
||||
faces[i].xvel = randomFloat(-0.1f, 0.1f);
|
||||
faces[i].yvel = randomFloat(-0.1f, 0.1f);
|
||||
faces[i].x = randomFloat(0.0f, w - HAPPY_FACE_SIZE);
|
||||
faces[i].y = randomFloat(0.0f, h - HAPPY_FACE_SIZE);
|
||||
faces[i].xvel = randomFloat(-60.0f, 60.0f);
|
||||
faces[i].yvel = randomFloat(-60.0f, 60.0f);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
render(SDL_Renderer *renderer)
|
||||
render(SDL_Renderer *renderer, double deltaTime)
|
||||
{
|
||||
|
||||
int i;
|
||||
SDL_Rect srcRect;
|
||||
SDL_Rect dstRect;
|
||||
int w;
|
||||
int h;
|
||||
|
||||
SDL_RenderGetLogicalSize(renderer, &w, &h);
|
||||
|
||||
/* setup boundaries for happyface bouncing */
|
||||
Uint16 maxx = SCREEN_WIDTH - HAPPY_FACE_SIZE;
|
||||
Uint16 maxy = SCREEN_HEIGHT - HAPPY_FACE_SIZE;
|
||||
Uint16 minx = 0;
|
||||
Uint16 miny = 0;
|
||||
int maxx = w - HAPPY_FACE_SIZE;
|
||||
int maxy = h - HAPPY_FACE_SIZE;
|
||||
int minx = 0;
|
||||
int miny = 0;
|
||||
|
||||
/* setup rects for drawing */
|
||||
srcRect.x = 0;
|
||||
|
@ -68,8 +74,8 @@ render(SDL_Renderer *renderer)
|
|||
- draw
|
||||
*/
|
||||
for (i = 0; i < NUM_HAPPY_FACES; i++) {
|
||||
faces[i].x += faces[i].xvel * MILLESECONDS_PER_FRAME;
|
||||
faces[i].y += faces[i].yvel * MILLESECONDS_PER_FRAME;
|
||||
faces[i].x += faces[i].xvel * deltaTime;
|
||||
faces[i].y += faces[i].yvel * deltaTime;
|
||||
if (faces[i].x > maxx) {
|
||||
faces[i].x = maxx;
|
||||
faces[i].xvel = -faces[i].xvel;
|
||||
|
@ -123,48 +129,45 @@ initializeTexture(SDL_Renderer *renderer)
|
|||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
|
||||
SDL_Window *window;
|
||||
SDL_Renderer *renderer;
|
||||
Uint32 startFrame;
|
||||
Uint32 endFrame;
|
||||
Uint32 delay;
|
||||
int done;
|
||||
int width;
|
||||
int height;
|
||||
|
||||
/* initialize SDL */
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||
fatalError("Could not initialize SDL");
|
||||
}
|
||||
window = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
|
||||
SDL_WINDOW_OPENGL |
|
||||
SDL_WINDOW_BORDERLESS);
|
||||
|
||||
/* The specified window size doesn't matter - except for its aspect ratio,
|
||||
* which determines whether the window is in portrait or landscape on iOS
|
||||
* (if SDL_WINDOW_RESIZABLE isn't specified). */
|
||||
window = SDL_CreateWindow(NULL, 0, 0, 320, 480, SDL_WINDOW_FULLSCREEN | SDL_WINDOW_ALLOW_HIGHDPI);
|
||||
|
||||
renderer = SDL_CreateRenderer(window, -1, 0);
|
||||
|
||||
SDL_GetWindowSize(window, &width, &height);
|
||||
SDL_RenderSetLogicalSize(renderer, width, height);
|
||||
|
||||
initializeTexture(renderer);
|
||||
initializeHappyFaces();
|
||||
initializeHappyFaces(renderer);
|
||||
|
||||
|
||||
/* main loop */
|
||||
done = 0;
|
||||
while (!done) {
|
||||
SDL_Event event;
|
||||
startFrame = SDL_GetTicks();
|
||||
double deltaTime = updateDeltaTime();
|
||||
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (event.type == SDL_QUIT) {
|
||||
done = 1;
|
||||
}
|
||||
}
|
||||
render(renderer);
|
||||
endFrame = SDL_GetTicks();
|
||||
|
||||
/* figure out how much time we have left, and then sleep */
|
||||
delay = MILLESECONDS_PER_FRAME - (endFrame - startFrame);
|
||||
if (delay < 0) {
|
||||
delay = 0;
|
||||
} else if (delay > MILLESECONDS_PER_FRAME) {
|
||||
delay = MILLESECONDS_PER_FRAME;
|
||||
}
|
||||
SDL_Delay(delay);
|
||||
render(renderer, deltaTime);
|
||||
SDL_Delay(1);
|
||||
}
|
||||
|
||||
/* cleanup */
|
||||
|
|
|
@ -132,10 +132,13 @@ keyToIndex(SDL_Keysym key)
|
|||
void
|
||||
getPositionForCharNumber(int n, int *x, int *y)
|
||||
{
|
||||
int renderW, renderH;
|
||||
SDL_RenderGetLogicalSize(renderer, &renderW, &renderH);
|
||||
|
||||
int x_padding = 16; /* padding space on left and right side of screen */
|
||||
int y_padding = 32; /* padding space at top of screen */
|
||||
/* figure out the number of characters that can fit horizontally across the screen */
|
||||
int max_x_chars = (SCREEN_WIDTH - 2 * x_padding) / GLYPH_SIZE_SCREEN;
|
||||
int max_x_chars = (renderW - 2 * x_padding) / GLYPH_SIZE_SCREEN;
|
||||
int line_separation = 5; /* pixels between each line */
|
||||
*x = (n % max_x_chars) * GLYPH_SIZE_SCREEN + x_padding;
|
||||
*y = (n / max_x_chars) * (GLYPH_SIZE_SCREEN + line_separation) +
|
||||
|
@ -228,21 +231,25 @@ loadFont(void)
|
|||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
|
||||
int index; /* index of last key we pushed in the bitmap font */
|
||||
SDL_Window *window;
|
||||
SDL_Event event; /* last event received */
|
||||
SDL_Keymod mod; /* key modifiers of last key we pushed */
|
||||
SDL_Scancode scancode; /* scancode of last key we pushed */
|
||||
int width;
|
||||
int height;
|
||||
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||
printf("Error initializing SDL: %s", SDL_GetError());
|
||||
}
|
||||
/* create window */
|
||||
window = SDL_CreateWindow("iPhone keyboard test", 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0);
|
||||
window = SDL_CreateWindow("iPhone keyboard test", 0, 0, 320, 480, SDL_WINDOW_ALLOW_HIGHDPI);
|
||||
/* create renderer */
|
||||
renderer = SDL_CreateRenderer(window, -1, 0);
|
||||
|
||||
SDL_GetWindowSize(window, &width, &height);
|
||||
SDL_RenderSetLogicalSize(renderer, width, height);
|
||||
|
||||
/* load up our font */
|
||||
loadFont();
|
||||
|
||||
|
|
|
@ -33,7 +33,7 @@ static struct sound drums[NUM_DRUMS];
|
|||
void handleMouseButtonDown(SDL_Event * event);
|
||||
void handleMouseButtonUp(SDL_Event * event);
|
||||
int playSound(struct sound *);
|
||||
void initializeButtons();
|
||||
void initializeButtons(SDL_Renderer *);
|
||||
void audioCallback(void *userdata, Uint8 * stream, int len);
|
||||
void loadSound(const char *file, struct sound *s);
|
||||
|
||||
|
@ -52,19 +52,21 @@ struct
|
|||
|
||||
/* sets up the buttons (color, position, state) */
|
||||
void
|
||||
initializeButtons()
|
||||
initializeButtons(SDL_Renderer *renderer)
|
||||
{
|
||||
|
||||
int i;
|
||||
int spacing = 10; /* gap between drum buttons */
|
||||
SDL_Rect buttonRect; /* keeps track of where to position drum */
|
||||
SDL_Color upColor = { 86, 86, 140, 255 }; /* color of drum when not pressed */
|
||||
SDL_Color downColor = { 191, 191, 221, 255 }; /* color of drum when pressed */
|
||||
int renderW, renderH;
|
||||
|
||||
SDL_RenderGetLogicalSize(renderer, &renderW, &renderH);
|
||||
|
||||
buttonRect.x = spacing;
|
||||
buttonRect.y = spacing;
|
||||
buttonRect.w = SCREEN_WIDTH - 2 * spacing;
|
||||
buttonRect.h = (SCREEN_HEIGHT - (NUM_DRUMS + 1) * spacing) / NUM_DRUMS;
|
||||
buttonRect.w = renderW - 2 * spacing;
|
||||
buttonRect.h = (renderH - (NUM_DRUMS + 1) * spacing) / NUM_DRUMS;
|
||||
|
||||
/* setup each button */
|
||||
for (i = 0; i < NUM_DRUMS; i++) {
|
||||
|
@ -270,24 +272,23 @@ audioCallback(void *userdata, Uint8 * stream, int len)
|
|||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
|
||||
int done; /* has user tried to quit ? */
|
||||
SDL_Window *window; /* main window */
|
||||
SDL_Renderer *renderer;
|
||||
SDL_Event event;
|
||||
Uint32 startFrame; /* holds when frame started processing */
|
||||
Uint32 endFrame; /* holds when frame ended processing */
|
||||
Uint32 delay; /* calculated delay, how long should we wait before next frame? */
|
||||
int i;
|
||||
int width;
|
||||
int height;
|
||||
|
||||
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
|
||||
fatalError("could not initialize SDL");
|
||||
}
|
||||
window =
|
||||
SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
|
||||
SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS);
|
||||
window = SDL_CreateWindow(NULL, 0, 0, 320, 480, SDL_WINDOW_BORDERLESS | SDL_WINDOW_ALLOW_HIGHDPI);
|
||||
renderer = SDL_CreateRenderer(window, 0, 0);
|
||||
|
||||
SDL_GetWindowSize(window, &width, &height);
|
||||
SDL_RenderSetLogicalSize(renderer, width, height);
|
||||
|
||||
/* initialize the mixer */
|
||||
SDL_memset(&mixer, 0, sizeof(mixer));
|
||||
/* setup output format */
|
||||
|
@ -310,12 +311,11 @@ main(int argc, char *argv[])
|
|||
loadSound("ds_china.wav", &drums[0]);
|
||||
|
||||
/* setup positions, colors, and state of buttons */
|
||||
initializeButtons();
|
||||
initializeButtons(renderer);
|
||||
|
||||
/* enter main loop */
|
||||
done = 0;
|
||||
while (!done) {
|
||||
startFrame = SDL_GetTicks();
|
||||
while (SDL_PollEvent(&event)) {
|
||||
switch (event.type) {
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
|
@ -330,16 +330,8 @@ main(int argc, char *argv[])
|
|||
}
|
||||
}
|
||||
render(renderer); /* draw buttons */
|
||||
endFrame = SDL_GetTicks();
|
||||
|
||||
/* figure out how much time we have left, and then sleep */
|
||||
delay = MILLESECONDS_PER_FRAME - (endFrame - startFrame);
|
||||
if (delay < 0) {
|
||||
delay = 0;
|
||||
} else if (delay > MILLESECONDS_PER_FRAME) {
|
||||
delay = MILLESECONDS_PER_FRAME;
|
||||
}
|
||||
SDL_Delay(delay);
|
||||
SDL_Delay(1);
|
||||
}
|
||||
|
||||
/* cleanup code, let's free up those sound buffers */
|
||||
|
|
|
@ -11,14 +11,18 @@
|
|||
void
|
||||
render(SDL_Renderer *renderer)
|
||||
{
|
||||
|
||||
Uint8 r, g, b;
|
||||
int renderW;
|
||||
int renderH;
|
||||
|
||||
SDL_RenderGetLogicalSize(renderer, &renderW, &renderH);
|
||||
|
||||
/* Come up with a random rectangle */
|
||||
SDL_Rect rect;
|
||||
rect.w = randomInt(64, 128);
|
||||
rect.h = randomInt(64, 128);
|
||||
rect.x = randomInt(0, SCREEN_WIDTH);
|
||||
rect.y = randomInt(0, SCREEN_HEIGHT);
|
||||
rect.x = randomInt(0, renderW);
|
||||
rect.y = randomInt(0, renderH);
|
||||
|
||||
/* Come up with a random color */
|
||||
r = randomInt(50, 255);
|
||||
|
@ -31,7 +35,6 @@ render(SDL_Renderer *renderer)
|
|||
|
||||
/* update screen */
|
||||
SDL_RenderPresent(renderer);
|
||||
|
||||
}
|
||||
|
||||
int
|
||||
|
@ -42,6 +45,8 @@ main(int argc, char *argv[])
|
|||
SDL_Renderer *renderer;
|
||||
int done;
|
||||
SDL_Event event;
|
||||
int windowW;
|
||||
int windowH;
|
||||
|
||||
/* initialize SDL */
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||
|
@ -52,7 +57,7 @@ main(int argc, char *argv[])
|
|||
srand(time(NULL));
|
||||
|
||||
/* create window and renderer */
|
||||
window = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0);
|
||||
window = SDL_CreateWindow(NULL, 0, 0, 320, 480, SDL_WINDOW_ALLOW_HIGHDPI);
|
||||
if (window == 0) {
|
||||
fatalError("Could not initialize Window");
|
||||
}
|
||||
|
@ -61,6 +66,9 @@ main(int argc, char *argv[])
|
|||
fatalError("Could not create renderer");
|
||||
}
|
||||
|
||||
SDL_GetWindowSize(window, &windowW, &windowH);
|
||||
SDL_RenderSetLogicalSize(renderer, windowW, windowH);
|
||||
|
||||
/* Fill screen with black */
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
||||
SDL_RenderClear(renderer);
|
||||
|
|
|
@ -82,6 +82,7 @@ main(int argc, char *argv[])
|
|||
SDL_Window *window; /* main window */
|
||||
SDL_Renderer *renderer;
|
||||
int done; /* does user want to quit? */
|
||||
int w, h;
|
||||
|
||||
/* initialize SDL */
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||
|
@ -89,11 +90,12 @@ main(int argc, char *argv[])
|
|||
}
|
||||
|
||||
/* create main window and renderer */
|
||||
window = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
|
||||
SDL_WINDOW_OPENGL |
|
||||
SDL_WINDOW_BORDERLESS);
|
||||
window = SDL_CreateWindow(NULL, 0, 0, 320, 480, SDL_WINDOW_BORDERLESS | SDL_WINDOW_ALLOW_HIGHDPI);
|
||||
renderer = SDL_CreateRenderer(window, 0, 0);
|
||||
|
||||
SDL_GetWindowSize(window, &w, &h);
|
||||
SDL_RenderSetLogicalSize(renderer, w, h);
|
||||
|
||||
/* load brush texture */
|
||||
initializeTexture(renderer);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue