mirror of
https://github.com/love2d/megasource.git
synced 2026-08-15 15:51:13 +02:00
Update SDL to 2.0.7
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
*/
|
||||
|
||||
#include "SDL.h"
|
||||
#include "math.h"
|
||||
#include <math.h>
|
||||
#include "common.h"
|
||||
|
||||
#define DAMPING 0.5f; /* after bouncing off a wall, damping coefficient determines final speed */
|
||||
|
||||
@@ -39,7 +39,7 @@ fatalError(const char *string)
|
||||
static Uint64 prevTime = 0;
|
||||
|
||||
double
|
||||
updateDeltaTime()
|
||||
updateDeltaTime(void)
|
||||
{
|
||||
Uint64 curTime;
|
||||
double deltaTime;
|
||||
|
||||
@@ -7,4 +7,4 @@
|
||||
extern int randomInt(int min, int max);
|
||||
extern float randomFloat(float min, float max);
|
||||
extern void fatalError(const char *string);
|
||||
extern double updateDeltaTime();
|
||||
extern double updateDeltaTime(void);
|
||||
|
||||
@@ -365,9 +365,6 @@ main(int argc, char *argv[])
|
||||
SDL_Window *window; /* main window */
|
||||
SDL_GLContext context;
|
||||
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 */
|
||||
int done; /* should we clean up and exit? */
|
||||
|
||||
/* initialize SDL */
|
||||
|
||||
@@ -7,20 +7,21 @@
|
||||
#include "SDL.h"
|
||||
#include "common.h"
|
||||
|
||||
#define TEST_INPUT_RECT
|
||||
|
||||
#define GLYPH_SIZE_IMAGE 16 /* size of glyphs (characters) in the bitmap font file */
|
||||
#define GLYPH_SIZE_SCREEN 32 /* size of glyphs (characters) as shown on the screen */
|
||||
|
||||
static SDL_Texture *texture; /* texture where we'll hold our font */
|
||||
#define MAX_CHARS 1024
|
||||
|
||||
/* function declarations */
|
||||
void cleanup(void);
|
||||
void drawBlank(int x, int y);
|
||||
static SDL_Texture *texture; /* texture where we'll hold our font */
|
||||
|
||||
static SDL_Renderer *renderer;
|
||||
static int numChars = 0; /* number of characters we've typed so far */
|
||||
static SDL_bool lastCharWasColon = 0; /* we use this to detect sequences such as :) */
|
||||
static SDL_Color bg_color = { 50, 50, 100, 255 }; /* color of background */
|
||||
|
||||
static int glyphs[MAX_CHARS];
|
||||
|
||||
/* this structure maps a scancode to an index in our bitmap font.
|
||||
it also contains data about under which modifiers the mapping is valid
|
||||
(for example, we don't want shift + 1 to produce the character '1',
|
||||
@@ -107,7 +108,7 @@ fontMapping map[TABLE_SIZE] = {
|
||||
If there is no entry for the key, -1 is returned
|
||||
*/
|
||||
int
|
||||
keyToIndex(SDL_Keysym key)
|
||||
keyToGlyphIndex(SDL_Keysym key)
|
||||
{
|
||||
int i, index = -1;
|
||||
for (i = 0; i < TABLE_SIZE; i++) {
|
||||
@@ -141,60 +142,27 @@ getPositionForCharNumber(int n, int *x, int *y)
|
||||
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) +
|
||||
y_padding;
|
||||
#ifdef TEST_INPUT_RECT
|
||||
*y = renderH - GLYPH_SIZE_SCREEN;
|
||||
#else
|
||||
*y = (n / max_x_chars) * (GLYPH_SIZE_SCREEN + line_separation) + y_padding;
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
drawIndex(int index)
|
||||
drawGlyph(int glyph, int positionIndex)
|
||||
{
|
||||
int x, y;
|
||||
getPositionForCharNumber(numChars, &x, &y);
|
||||
SDL_Rect srcRect =
|
||||
{ GLYPH_SIZE_IMAGE * index, 0, GLYPH_SIZE_IMAGE, GLYPH_SIZE_IMAGE };
|
||||
getPositionForCharNumber(positionIndex, &x, &y);
|
||||
SDL_Rect srcRect = { GLYPH_SIZE_IMAGE * glyph, 0, GLYPH_SIZE_IMAGE, GLYPH_SIZE_IMAGE };
|
||||
SDL_Rect dstRect = { x, y, GLYPH_SIZE_SCREEN, GLYPH_SIZE_SCREEN };
|
||||
drawBlank(x, y);
|
||||
SDL_RenderCopy(renderer, texture, &srcRect, &dstRect);
|
||||
}
|
||||
|
||||
/* draws the cursor icon at the current end position of the text */
|
||||
void
|
||||
drawCursor(void)
|
||||
{
|
||||
drawIndex(29); /* cursor is at index 29 in the bitmap font */
|
||||
}
|
||||
|
||||
/* paints over a glyph sized region with the background color
|
||||
in effect it erases the area
|
||||
*/
|
||||
void
|
||||
drawBlank(int x, int y)
|
||||
{
|
||||
SDL_Rect rect = { x, y, GLYPH_SIZE_SCREEN, GLYPH_SIZE_SCREEN };
|
||||
SDL_SetRenderDrawColor(renderer, bg_color.r, bg_color.g, bg_color.b, bg_color.a);
|
||||
SDL_RenderFillRect(renderer, &rect);
|
||||
}
|
||||
|
||||
/* moves backwards one character, erasing the last one put down */
|
||||
void
|
||||
backspace(void)
|
||||
{
|
||||
int x, y;
|
||||
if (numChars > 0) {
|
||||
getPositionForCharNumber(numChars, &x, &y);
|
||||
drawBlank(x, y);
|
||||
numChars--;
|
||||
getPositionForCharNumber(numChars, &x, &y);
|
||||
drawBlank(x, y);
|
||||
drawCursor();
|
||||
}
|
||||
}
|
||||
|
||||
/* this function loads our font into an SDL_Texture and returns the SDL_Texture */
|
||||
SDL_Texture*
|
||||
loadFont(void)
|
||||
{
|
||||
|
||||
SDL_Surface *surface = SDL_LoadBMP("kromasky_16x16.bmp");
|
||||
|
||||
if (!surface) {
|
||||
@@ -214,8 +182,7 @@ loadFont(void)
|
||||
Bmask, Amask);
|
||||
SDL_BlitSurface(surface, NULL, converted, NULL);
|
||||
/* create our texture */
|
||||
texture =
|
||||
SDL_CreateTextureFromSurface(renderer, converted);
|
||||
texture = SDL_CreateTextureFromSurface(renderer, converted);
|
||||
if (texture == 0) {
|
||||
printf("texture creation failed: %s\n", SDL_GetError());
|
||||
} else {
|
||||
@@ -228,90 +195,107 @@ loadFont(void)
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
draw()
|
||||
{
|
||||
SDL_SetRenderDrawColor(renderer, bg_color.r, bg_color.g, bg_color.b, bg_color.a);
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
for (int i = 0; i < numChars; i++) {
|
||||
drawGlyph(glyphs[i], i);
|
||||
}
|
||||
|
||||
drawGlyph(29, numChars); /* cursor is at index 29 in the bitmap font */
|
||||
|
||||
SDL_RenderPresent(renderer);
|
||||
}
|
||||
|
||||
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;
|
||||
int done;
|
||||
SDL_Rect textrect;
|
||||
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||
printf("Error initializing SDL: %s", SDL_GetError());
|
||||
}
|
||||
/* create window */
|
||||
window = SDL_CreateWindow("iPhone keyboard test", 0, 0, 320, 480, SDL_WINDOW_ALLOW_HIGHDPI);
|
||||
window = SDL_CreateWindow("iOS keyboard test", 0, 0, 0, 0, SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
|
||||
/* create renderer */
|
||||
renderer = SDL_CreateRenderer(window, -1, 0);
|
||||
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);
|
||||
|
||||
SDL_GetWindowSize(window, &width, &height);
|
||||
SDL_RenderSetLogicalSize(renderer, width, height);
|
||||
|
||||
/* load up our font */
|
||||
loadFont();
|
||||
|
||||
/* Show onscreen keyboard */
|
||||
#ifdef TEST_INPUT_RECT
|
||||
textrect.x = 0;
|
||||
textrect.y = height - GLYPH_SIZE_IMAGE;
|
||||
textrect.w = width;
|
||||
textrect.h = GLYPH_SIZE_IMAGE;
|
||||
SDL_SetTextInputRect(&textrect);
|
||||
#endif
|
||||
SDL_StartTextInput();
|
||||
|
||||
/* draw the background, we'll just paint over it */
|
||||
SDL_SetRenderDrawColor(renderer, bg_color.r, bg_color.g, bg_color.b, bg_color.a);
|
||||
SDL_RenderFillRect(renderer, NULL);
|
||||
SDL_RenderPresent(renderer);
|
||||
|
||||
int done = 0;
|
||||
/* loop till we get SDL_Quit */
|
||||
while (!done && SDL_WaitEvent(&event)) {
|
||||
switch (event.type) {
|
||||
case SDL_QUIT:
|
||||
done = 1;
|
||||
break;
|
||||
case SDL_KEYDOWN:
|
||||
index = keyToIndex(event.key.keysym);
|
||||
scancode = event.key.keysym.scancode;
|
||||
mod = event.key.keysym.mod;
|
||||
if (scancode == SDL_SCANCODE_DELETE) {
|
||||
/* if user hit delete, delete the last character */
|
||||
backspace();
|
||||
lastCharWasColon = 0;
|
||||
} else if (lastCharWasColon && scancode == SDL_SCANCODE_0
|
||||
&& (mod & KMOD_SHIFT)) {
|
||||
/* if our last key was a colon and this one is a close paren, the make a hoppy face */
|
||||
backspace();
|
||||
drawIndex(32); /* index for happy face */
|
||||
numChars++;
|
||||
drawCursor();
|
||||
lastCharWasColon = 0;
|
||||
} else if (index != -1) {
|
||||
/* if we aren't doing a happy face, then just draw the normal character */
|
||||
drawIndex(index);
|
||||
numChars++;
|
||||
drawCursor();
|
||||
lastCharWasColon =
|
||||
(event.key.keysym.scancode == SDL_SCANCODE_SEMICOLON
|
||||
&& (event.key.keysym.mod & KMOD_SHIFT));
|
||||
done = 0;
|
||||
while (!done) {
|
||||
while (SDL_PollEvent(&event)) {
|
||||
switch (event.type) {
|
||||
case SDL_QUIT:
|
||||
done = 1;
|
||||
break;
|
||||
case SDL_WINDOWEVENT:
|
||||
if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
|
||||
width = event.window.data1;
|
||||
height = event.window.data2;
|
||||
SDL_RenderSetLogicalSize(renderer, width, height);
|
||||
#ifdef TEST_INPUT_RECT
|
||||
textrect.x = 0;
|
||||
textrect.y = height - GLYPH_SIZE_IMAGE;
|
||||
textrect.w = width;
|
||||
textrect.h = GLYPH_SIZE_IMAGE;
|
||||
SDL_SetTextInputRect(&textrect);
|
||||
#endif
|
||||
}
|
||||
break;
|
||||
case SDL_KEYDOWN:
|
||||
if (event.key.keysym.scancode == SDL_SCANCODE_BACKSPACE) {
|
||||
if (numChars > 0) {
|
||||
numChars--;
|
||||
}
|
||||
} else if (numChars + 1 < MAX_CHARS) {
|
||||
int index = keyToGlyphIndex(event.key.keysym);
|
||||
if (index >= 0) {
|
||||
glyphs[numChars++] = index;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case SDL_MOUSEBUTTONUP:
|
||||
/* mouse up toggles onscreen keyboard visibility */
|
||||
if (SDL_IsTextInputActive()) {
|
||||
SDL_StopTextInput();
|
||||
} else {
|
||||
SDL_StartTextInput();
|
||||
}
|
||||
break;
|
||||
}
|
||||
/* check if the key was a colon */
|
||||
/* draw our updates to the screen */
|
||||
SDL_RenderPresent(renderer);
|
||||
break;
|
||||
case SDL_MOUSEBUTTONUP:
|
||||
/* mouse up toggles onscreen keyboard visibility */
|
||||
if (SDL_IsTextInputActive()) {
|
||||
SDL_StopTextInput();
|
||||
} else {
|
||||
SDL_StartTextInput();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
draw();
|
||||
SDL_Delay(15);
|
||||
}
|
||||
cleanup();
|
||||
|
||||
SDL_DestroyTexture(texture);
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* clean up after ourselves like a good kiddy */
|
||||
void
|
||||
cleanup(void)
|
||||
{
|
||||
SDL_DestroyTexture(texture);
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
@@ -250,7 +250,7 @@ audioCallback(void *userdata, Uint8 * stream, int len)
|
||||
|
||||
/* mix this sound effect with the output */
|
||||
SDL_MixAudioFormat(stream, mixer.channels[i].position,
|
||||
mixer.outputSpec.format, copy_amt, 150);
|
||||
mixer.outputSpec.format, copy_amt, SDL_MIX_MAXVOLUME);
|
||||
|
||||
/* update buffer position in sound effect and the number of bytes left */
|
||||
mixer.channels[i].position += copy_amt;
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
*/
|
||||
|
||||
#include "SDL.h"
|
||||
#include "math.h"
|
||||
#include <math.h>
|
||||
#include "common.h"
|
||||
|
||||
#define BRUSH_SIZE 32 /* width and height of the brush */
|
||||
|
||||
Reference in New Issue
Block a user