whatever: converted to SDL2
This commit is contained in:
parent
32a27b2d68
commit
85f2956c67
@ -11,7 +11,7 @@ CC = clang
|
|||||||
COMPILER_FLAGS = -Wall
|
COMPILER_FLAGS = -Wall
|
||||||
|
|
||||||
#LINKER_FLAGS specifies the libraries we're linking against
|
#LINKER_FLAGS specifies the libraries we're linking against
|
||||||
LINKER_FLAGS = -lSDL
|
LINKER_FLAGS = -lSDL2
|
||||||
|
|
||||||
#OBJ_NAME specifies the name of our executable
|
#OBJ_NAME specifies the name of our executable
|
||||||
OBJ_NAME= whatever
|
OBJ_NAME= whatever
|
||||||
|
32
sdl/whatever/Makefile.sdl1
Normal file
32
sdl/whatever/Makefile.sdl1
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
PREFIX=/usr
|
||||||
|
|
||||||
|
#OBJS specifies which files to compile as part of the project
|
||||||
|
OBJS = whatever.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= whatever
|
||||||
|
|
||||||
|
#This is the target that compiles our executable
|
||||||
|
all : $(OBJS)
|
||||||
|
$(CC) $(OBJS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME)
|
||||||
|
install :
|
||||||
|
mkdir -p ${PREFIX}/share/${OBJ_NAME}
|
||||||
|
cp ${OBJ_NAME} ${PREFIX}/share/${OBJ_NAME}/
|
||||||
|
ln -si ${PREFIX}/share/${OBJ_NAME}/${OBJ_NAME} ${PREFIX}/bin/${OBJ_NAME}
|
||||||
|
uninstall :
|
||||||
|
rm ${PREFIX}/bin/${OBJ_NAME}
|
||||||
|
rm ${PREFIX}/share/${OBJ_NAME}/${OBJ_NAME}
|
||||||
|
rm -r ${PREFIX}/share/${OBJ_NAME}
|
||||||
|
clean :
|
||||||
|
rm ${OBJ_NAME}
|
||||||
|
|
Binary file not shown.
@ -1,4 +1,4 @@
|
|||||||
#include "SDL/SDL.h"
|
#include "SDL2/SDL.h"
|
||||||
|
|
||||||
//Write a function that moves an image based on which arrow keys are pressed.
|
//Write a function that moves an image based on which arrow keys are pressed.
|
||||||
//something like
|
//something like
|
||||||
@ -10,53 +10,66 @@
|
|||||||
int main ( int argc, char *argv[] )
|
int main ( int argc, char *argv[] )
|
||||||
{
|
{
|
||||||
/* initialize SDL */
|
/* initialize SDL */
|
||||||
if (SDL_Init(SDL_INIT_VIDEO) != 0){
|
if (SDL_Init(SDL_INIT_EVERYTHING) != 0){
|
||||||
printf("Unable to init SDL!: %s\n", SDL_GetError());
|
printf("Unable to init SDL!: %s\n", SDL_GetError());
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* set the title bar */
|
|
||||||
SDL_WM_SetCaption("SDL Test", "SDL Test");
|
|
||||||
|
|
||||||
/* init surfaces */
|
/* init surfaces */
|
||||||
SDL_Surface* screen;
|
SDL_Window* screen; //No longer need to do this part I suppose
|
||||||
|
SDL_Renderer* renderer;
|
||||||
SDL_Surface* temp;
|
SDL_Surface* temp;
|
||||||
SDL_Surface* bg;
|
SDL_Texture* top_image;
|
||||||
SDL_Surface* top_image;
|
|
||||||
SDL_Rect src, dest;
|
const char background_imagefile[] = "sdl_logo.bmp";
|
||||||
|
SDL_Surface* background_surface;
|
||||||
|
SDL_Texture* background_texture;
|
||||||
|
SDL_Rect top_image_dest = { 0 };
|
||||||
|
SDL_Event event = { 0 };
|
||||||
|
|
||||||
|
int screen_width = 1024, screen_height = 768;
|
||||||
|
int top_image_width = 0, top_image_height = 0;
|
||||||
|
|
||||||
|
|
||||||
/* create window of size 640x480 in hi-color (16-bit) mode*/
|
|
||||||
screen = SDL_SetVideoMode(640, 480, 16, 0);
|
/* create window of size 640x480 and set title bar*/
|
||||||
|
screen = SDL_CreateWindow("SDL TEST",
|
||||||
|
SDL_WINDOWPOS_CENTERED,
|
||||||
|
SDL_WINDOWPOS_CENTERED,
|
||||||
|
screen_width, screen_height,
|
||||||
|
0);
|
||||||
|
|
||||||
if (screen == NULL){
|
if (screen == NULL){
|
||||||
printf("Unable to set video mode: %s\n", SDL_GetError());
|
printf("Unable to set video mode: %s\n", SDL_GetError());
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
renderer = SDL_CreateRenderer(screen, -1, 0);
|
||||||
|
|
||||||
|
if (renderer == NULL){
|
||||||
|
printf("Unable to create renderer: %s\n", SDL_GetError());
|
||||||
|
}
|
||||||
|
|
||||||
atexit(SDL_Quit);
|
atexit(SDL_Quit);
|
||||||
|
|
||||||
/* load bitmap to temp surface */
|
/* load bitmap to surface */
|
||||||
temp = SDL_LoadBMP("sdl_logo.bmp");
|
background_surface = SDL_LoadBMP(background_imagefile);
|
||||||
|
|
||||||
if (temp == NULL){
|
if (background_surface == NULL){
|
||||||
printf("Unable to find image!!: %s\n", SDL_GetError());
|
printf("Unable to find image!!: %s\n", SDL_GetError());
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
else{
|
||||||
/* convert bitmap to display format */
|
/* convert bitmap to texture */
|
||||||
bg = SDL_DisplayFormat(temp);
|
background_texture = SDL_CreateTextureFromSurface(renderer, background_surface);
|
||||||
if (bg == NULL){
|
SDL_FreeSurface(background_surface); // No longer need the surface
|
||||||
printf("Unable to format image!: %s\n", SDL_GetError());
|
background_surface = NULL;
|
||||||
|
if(background_texture == NULL) {
|
||||||
|
printf("Failed to create texture from background surface: %s\n", SDL_GetError());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* free the temp surface
|
|
||||||
* What we are doing above, with temp is simply
|
|
||||||
* making everything easier to deal with
|
|
||||||
* then freeing the extra memory space.
|
|
||||||
* i.e: We could have just as easily done
|
|
||||||
* SDL_DisplayFormat(SDL_LoadBMP("sdl_logo.bmp"));*/
|
|
||||||
SDL_FreeSurface(temp);
|
|
||||||
|
|
||||||
/* load top_image image to practice blit on top of surface*/
|
/* load top_image image to practice blit on top of surface*/
|
||||||
temp = SDL_LoadBMP("dinowheel.bmp");
|
temp = SDL_LoadBMP("dinowheel.bmp");
|
||||||
@ -64,26 +77,19 @@ int main ( int argc, char *argv[] )
|
|||||||
printf("Unable to load top_image: %s/n", SDL_GetError());
|
printf("Unable to load top_image: %s/n", SDL_GetError());
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
else{
|
||||||
|
top_image = SDL_CreateTextureFromSurface(renderer, temp);
|
||||||
|
SDL_FreeSurface(temp);
|
||||||
|
temp = NULL;
|
||||||
|
/* Get height and width of top_image */
|
||||||
|
SDL_QueryTexture(top_image, NULL, NULL, &top_image_width, &top_image_height);
|
||||||
|
|
||||||
top_image = SDL_DisplayFormat(temp);
|
if(top_image == NULL) {
|
||||||
if (top_image == NULL){
|
printf("Failed to create texture from top_image: %s\n", SDL_GetError());
|
||||||
printf("Unable to format top_image: %s/n", SDL_GetError());
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
SDL_FreeSurface(temp);
|
|
||||||
|
|
||||||
src.x = 0;
|
|
||||||
src.y = 0;
|
|
||||||
src.w = top_image->w; /* copy entire image */
|
|
||||||
src.h = top_image->h;
|
|
||||||
/* Could also do "dest = src" if they were to start at the same spot */
|
|
||||||
dest.x = 25;
|
|
||||||
dest.y = 25;
|
|
||||||
dest.w = top_image->w;
|
|
||||||
dest.h = top_image->h;
|
|
||||||
|
|
||||||
SDL_Event event;
|
|
||||||
int gameover = 0;
|
int gameover = 0;
|
||||||
|
|
||||||
/* message pump */
|
/* message pump */
|
||||||
@ -113,24 +119,29 @@ int main ( int argc, char *argv[] )
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* draw the background */
|
|
||||||
SDL_BlitSurface(bg, NULL, screen, NULL);
|
|
||||||
|
|
||||||
/* Blit top_image to the screen */
|
/* render the background */
|
||||||
SDL_BlitSurface(top_image, &src, screen, &dest);
|
SDL_RenderCopy(renderer, background_texture, NULL, NULL);
|
||||||
|
|
||||||
/* update the screen */
|
/* Render entire top_image to center of screen */
|
||||||
SDL_UpdateRect(screen, 0, 0, 0, 0);
|
top_image_dest.x = (screen_width / 2) - (top_image_width / 2);
|
||||||
|
top_image_dest.y = (screen_width / 2) - (top_image_height / 2);
|
||||||
|
top_image_dest.w = top_image_width;
|
||||||
|
top_image_dest.h = top_image_height;
|
||||||
|
SDL_RenderCopy(renderer, top_image, NULL, &top_image_dest);
|
||||||
|
|
||||||
|
/* Render here */
|
||||||
|
SDL_RenderPresent(renderer);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* free the background surface */
|
|
||||||
SDL_FreeSurface(bg);
|
|
||||||
|
|
||||||
/* free top_image */
|
|
||||||
SDL_FreeSurface(top_image);
|
|
||||||
|
|
||||||
/* cleanup SDL */
|
/* cleanup SDL */
|
||||||
|
if (renderer) SDL_DestroyRenderer(renderer);
|
||||||
|
if (screen) SDL_DestroyWindow(screen);
|
||||||
|
if (background_texture) SDL_DestroyTexture(background_texture);
|
||||||
|
if (top_image) SDL_DestroyTexture(top_image);
|
||||||
|
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
136
sdl/whatever/whateverSDL1.c
Normal file
136
sdl/whatever/whateverSDL1.c
Normal file
@ -0,0 +1,136 @@
|
|||||||
|
#include "SDL/SDL.h"
|
||||||
|
|
||||||
|
//Write a function that moves an image based on which arrow keys are pressed.
|
||||||
|
//something like
|
||||||
|
//void move_image(direction, image);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int main ( int argc, char *argv[] )
|
||||||
|
{
|
||||||
|
/* initialize SDL */
|
||||||
|
if (SDL_Init(SDL_INIT_VIDEO) != 0){
|
||||||
|
printf("Unable to init SDL!: %s\n", SDL_GetError());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* set the title bar */
|
||||||
|
SDL_WM_SetCaption("SDL Test", "SDL Test");
|
||||||
|
|
||||||
|
/* init surfaces */
|
||||||
|
SDL_Surface* screen;
|
||||||
|
SDL_Surface* temp;
|
||||||
|
SDL_Surface* bg;
|
||||||
|
SDL_Surface* top_image;
|
||||||
|
SDL_Rect src, dest;
|
||||||
|
|
||||||
|
|
||||||
|
/* create window of size 640x480 in hi-color (16-bit) mode*/
|
||||||
|
screen = SDL_SetVideoMode(640, 480, 16, 0);
|
||||||
|
|
||||||
|
if (screen == NULL){
|
||||||
|
printf("Unable to set video mode: %s\n", SDL_GetError());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
atexit(SDL_Quit);
|
||||||
|
|
||||||
|
/* load bitmap to temp surface */
|
||||||
|
temp = SDL_LoadBMP("sdl_logo.bmp");
|
||||||
|
|
||||||
|
if (temp == NULL){
|
||||||
|
printf("Unable to find image!!: %s\n", SDL_GetError());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* convert bitmap to display format */
|
||||||
|
bg = SDL_DisplayFormat(temp);
|
||||||
|
if (bg == NULL){
|
||||||
|
printf("Unable to format image!: %s\n", SDL_GetError());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* free the temp surface
|
||||||
|
* What we are doing above, with temp is simply
|
||||||
|
* making everything easier to deal with
|
||||||
|
* then freeing the extra memory space.
|
||||||
|
* i.e: We could have just as easily done
|
||||||
|
* SDL_DisplayFormat(SDL_LoadBMP("sdl_logo.bmp"));*/
|
||||||
|
SDL_FreeSurface(temp);
|
||||||
|
|
||||||
|
/* load top_image image to practice blit on top of surface*/
|
||||||
|
temp = SDL_LoadBMP("dinowheel.bmp");
|
||||||
|
if (temp == NULL){
|
||||||
|
printf("Unable to load top_image: %s/n", SDL_GetError());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
top_image = SDL_DisplayFormat(temp);
|
||||||
|
if (top_image == NULL){
|
||||||
|
printf("Unable to format top_image: %s/n", SDL_GetError());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_FreeSurface(temp);
|
||||||
|
|
||||||
|
src.x = 0;
|
||||||
|
src.y = 0;
|
||||||
|
src.w = top_image->w; /* copy entire image */
|
||||||
|
src.h = top_image->h;
|
||||||
|
/* Could also do "dest = src" if they were to start at the same spot */
|
||||||
|
dest.x = 25;
|
||||||
|
dest.y = 25;
|
||||||
|
dest.w = top_image->w;
|
||||||
|
dest.h = top_image->h;
|
||||||
|
|
||||||
|
SDL_Event event;
|
||||||
|
int gameover = 0;
|
||||||
|
|
||||||
|
/* message pump */
|
||||||
|
while (!gameover)
|
||||||
|
{
|
||||||
|
/* look for an event */
|
||||||
|
if (SDL_PollEvent(&event)) {
|
||||||
|
/* an event was found */
|
||||||
|
switch (event.type) {
|
||||||
|
/* close button clicked */
|
||||||
|
case SDL_QUIT:
|
||||||
|
gameover = 1;
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* handle the keyboard */
|
||||||
|
case SDL_KEYDOWN:
|
||||||
|
switch (event.key.keysym.sym) {
|
||||||
|
case SDLK_ESCAPE:
|
||||||
|
case SDLK_q:
|
||||||
|
gameover = 1;
|
||||||
|
break;
|
||||||
|
case SDLK_w:
|
||||||
|
printf("You just pressed 'w'\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* draw the background */
|
||||||
|
SDL_BlitSurface(bg, NULL, screen, NULL);
|
||||||
|
|
||||||
|
/* Blit top_image to the screen */
|
||||||
|
SDL_BlitSurface(top_image, &src, screen, &dest);
|
||||||
|
|
||||||
|
/* update the screen */
|
||||||
|
SDL_UpdateRect(screen, 0, 0, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* free the background surface */
|
||||||
|
SDL_FreeSurface(bg);
|
||||||
|
|
||||||
|
/* free top_image */
|
||||||
|
SDL_FreeSurface(top_image);
|
||||||
|
|
||||||
|
/* cleanup SDL */
|
||||||
|
SDL_Quit();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user