init commit
This commit is contained in:
25
sdl/first/Makefile
Normal file
25
sdl/first/Makefile
Normal file
@ -0,0 +1,25 @@
|
||||
#OBJS specifies which files to compile as part of the project
|
||||
OBJS = main.c
|
||||
|
||||
#CC specifies which compiler to use
|
||||
CC = clang
|
||||
|
||||
#COMPILER_FLAGS specifies the additional compilation options we're using
|
||||
# -w suppress all warnings
|
||||
COMPILER_FLAGS = -Wall
|
||||
|
||||
#LINKER_FLAGS specifies the libraries we're linking against
|
||||
LINKER_FLAGS = -lSDL
|
||||
|
||||
#OBJ_NAME specifies the name of our executable
|
||||
OBJ_NAME= first
|
||||
|
||||
#This is the target that compiles our executable
|
||||
all : $(OBJS)
|
||||
$(CC) $(OBJS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME)
|
||||
clean :
|
||||
rm euhelp
|
||||
install :
|
||||
cp euhelp /usr/bin
|
||||
uninstall :
|
||||
rm /usr/bin/euhelp
|
BIN
sdl/first/bat.bmp
Normal file
BIN
sdl/first/bat.bmp
Normal file
Binary file not shown.
After Width: | Height: | Size: 251 KiB |
BIN
sdl/first/first
Executable file
BIN
sdl/first/first
Executable file
Binary file not shown.
116
sdl/first/main.c
Normal file
116
sdl/first/main.c
Normal file
@ -0,0 +1,116 @@
|
||||
#include "SDL/SDL.h"
|
||||
|
||||
#define TRUE 1
|
||||
#define FALSE 0
|
||||
|
||||
const int WINDOW_WIDTH = 640;
|
||||
const int WINDOW_HEIGHT = 480;
|
||||
const char* WINDOW_TITLE = "SDL Start";
|
||||
|
||||
void drawSprite(SDL_Surface* imageSurface,
|
||||
SDL_Surface* screenSurface,
|
||||
int srcX, int srcY,
|
||||
int dstX, int dstY,
|
||||
int width, int height);
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
SDL_Init( SDL_INIT_VIDEO );
|
||||
|
||||
SDL_Surface* screen = SDL_SetVideoMode( WINDOW_WIDTH, WINDOW_HEIGHT, 0,
|
||||
SDL_HWSURFACE | SDL_DOUBLEBUF );
|
||||
SDL_WM_SetCaption( WINDOW_TITLE, 0 );
|
||||
|
||||
SDL_Surface* bitmap = SDL_LoadBMP("bat.bmp");
|
||||
SDL_SetColorKey( bitmap, SDL_SRCCOLORKEY, SDL_MapRGB(bitmap->format, 255, 0, 255) );
|
||||
|
||||
int batImageX = 24;
|
||||
int batImageY = 63;
|
||||
int batWidth = 65;
|
||||
int batHeight = 44;
|
||||
|
||||
//bat pos
|
||||
int batX = 100;
|
||||
int batY = 100;
|
||||
|
||||
SDL_Event event;
|
||||
int gameRunning = TRUE;
|
||||
int keysHeld[323] = {FALSE};
|
||||
|
||||
while (gameRunning == TRUE)
|
||||
{
|
||||
// Handle input
|
||||
if (SDL_PollEvent(&event))
|
||||
{
|
||||
if (event.type == SDL_QUIT)
|
||||
{
|
||||
gameRunning = FALSE;
|
||||
}
|
||||
|
||||
if (event.type == SDL_KEYDOWN) {
|
||||
keysHeld[event.key.keysym.sym] = TRUE;
|
||||
}
|
||||
if (event.type == SDL_KEYUP)
|
||||
{
|
||||
keysHeld[event.key.keysym.sym] = FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
if ( keysHeld[SDLK_ESCAPE] )
|
||||
{
|
||||
gameRunning = FALSE;
|
||||
}
|
||||
if (keysHeld[SDLK_LEFT] )
|
||||
{
|
||||
batX -= 1;
|
||||
}
|
||||
if (keysHeld[SDLK_RIGHT] )
|
||||
{
|
||||
batX += 1;
|
||||
}
|
||||
if (keysHeld[SDLK_UP])
|
||||
{
|
||||
batY -= 1;
|
||||
}
|
||||
if (keysHeld[SDLK_DOWN])
|
||||
{
|
||||
batY += 1;
|
||||
}
|
||||
|
||||
// Draw scene
|
||||
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
|
||||
|
||||
drawSprite(bitmap,
|
||||
screen,
|
||||
batImageX, batImageY,
|
||||
batX, batY,
|
||||
batWidth, batHeight);
|
||||
SDL_Flip(screen);
|
||||
}
|
||||
|
||||
SDL_FreeSurface(bitmap);
|
||||
SDL_Quit();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void drawSprite(SDL_Surface* imageSurface,
|
||||
SDL_Surface* screenSurface,
|
||||
int srcX, int srcY,
|
||||
int dstX, int dstY,
|
||||
int width, int height)
|
||||
{
|
||||
SDL_Rect srcRect;
|
||||
srcRect.x = srcX;
|
||||
srcRect.y = srcY;
|
||||
srcRect.w = width;
|
||||
srcRect.h = height;
|
||||
|
||||
SDL_Rect dstRect;
|
||||
dstRect.x = dstX;
|
||||
dstRect.y = dstY;
|
||||
dstRect.w = width;
|
||||
dstRect.h = height;
|
||||
|
||||
SDL_BlitSurface(imageSurface, &srcRect, screenSurface, &dstRect);
|
||||
}
|
Reference in New Issue
Block a user