commit cb4eb030bd460bbd77bc219750dbac01ce8b6a42 Author: Logen Kain Date: Thu Jul 21 10:22:47 2016 -0700 init commit diff --git a/C_BOOK/1.5.1/a.out b/C_BOOK/1.5.1/a.out new file mode 100755 index 0000000..7f35f69 Binary files /dev/null and b/C_BOOK/1.5.1/a.out differ diff --git a/C_BOOK/1.5.1/main.c b/C_BOOK/1.5.1/main.c new file mode 100644 index 0000000..4bb319d --- /dev/null +++ b/C_BOOK/1.5.1/main.c @@ -0,0 +1,13 @@ +#define Q 113 +#include + +int main() +{ + int c; + while (c != 'q') { + + c = getchar(); + printf("%d\n",c); + } + return 0; +} diff --git a/C_BOOK/1.5.1/test.txt b/C_BOOK/1.5.1/test.txt new file mode 100644 index 0000000..8f7e42d --- /dev/null +++ b/C_BOOK/1.5.1/test.txt @@ -0,0 +1,2 @@ +bob is a faggot +bob is also an u. diff --git a/C_BOOK/1.9-arrays/main.c b/C_BOOK/1.9-arrays/main.c new file mode 100644 index 0000000..1b3b854 --- /dev/null +++ b/C_BOOK/1.9-arrays/main.c @@ -0,0 +1,52 @@ +#include +#define MAXLINE 1000 + +int getline( char line[], int maxline); +void copy(char to[], char from[]); + +/* print longest input line */ + +main() +{ + int len; /* current line length */ + int max; /* maximum length seen so far */ + char line[MAXLINE]; /* current input line */ + char longest[MAXLINE]; /* longest line saved here */ + + max = 0; + while ((len = getline(line, MAXLINE)) > 0) + if (len > max) { + max = len; + copy(longest, line); + } + if (max > 0) { /* there was a line */ + printf("%s", longest); + } + return 0; +} + +/* getline: read a line into s, return length */ +int getline(char s[], int lim) +{ + int c, i; + + for (i=0; i +#include +#include +void setup() { glClearColor(1.0f, 1.0f, 1.0f, 1.0f); } +void display() + { + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glColor3f(0.0f, 0.0f, 0.0f); + glRectf(-0.75f,0.75f, 0.75f, -0.75f); + glutSwapBuffers(); + } +int main(int argc, char *argv[]) + { + glutInit(&argc, argv); + glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); + glutInitWindowSize(800,600); + glutCreateWindow("Hello World"); + + setup(); + glutDisplayFunc(display); + glutMainLoop(); + return 0; + } diff --git a/c_graphics/sample/test.c b/c_graphics/sample/test.c new file mode 100644 index 0000000..47f36a2 --- /dev/null +++ b/c_graphics/sample/test.c @@ -0,0 +1,14 @@ +#include +#include + +int main() +{ + int gd = DETECT, gm; + + initgraph(&gd, &gm, "C:\\TC\\BGI"); + + getch(); + closegraph(); + return0 +} + diff --git a/curses/example_1/Makefile b/curses/example_1/Makefile new file mode 100644 index 0000000..df18b6e --- /dev/null +++ b/curses/example_1/Makefile @@ -0,0 +1,3 @@ +LDFLAGS=-lncurses + +all: demo diff --git a/curses/example_1/demo b/curses/example_1/demo new file mode 100755 index 0000000..b104ce4 Binary files /dev/null and b/curses/example_1/demo differ diff --git a/curses/example_1/demo.c b/curses/example_1/demo.c new file mode 100644 index 0000000..41018d8 --- /dev/null +++ b/curses/example_1/demo.c @@ -0,0 +1,92 @@ +#include +#include +#include + +#define DELAY 30000 + +void quit(); +void draw_borders(WINDOW *screen); + +int main(int argc, char *argv[]){ + int x=0, y=0; + int max_y = 0, max_x = 0; + int next_x = 0; + int direction = 1; + int keypress = 0; + + initscr(); // Initialize window + noecho(); // Don't echo keypresses + curs_set(FALSE); // No cursor + nodelay(stdscr,1); // Stop getch from waiting. + //Global var 'stdscr' is created by the call to 'initscr()' + getmaxyx(stdscr, max_y, max_x); //Get the max x/y of the screen + + WINDOW *main = newwin(max_y - 3, max_x, 0, 0); + WINDOW *sub = newwin(3, max_x, max_y - 3, 0); + draw_borders(main); + draw_borders(sub); + + + + while(1) { + getmaxyx(stdscr, max_y, max_x); + keypress = getch(); // Listen for exit command + clear(); // Clear screen of all previous chars + mvprintw(y, x, "o"); // Print our "ball" at the current xy position + refresh(); + + usleep(DELAY); // Shorter delay between movements + + next_x = x + direction; + + if (keypress == 'q') { + quit(); + } + + if (next_x >= max_x || next_x < 0) { + direction*= -1; + } + else { + x+= direction; + } + mvwin(sub, 50, 50); + wrefresh(sub); + +// x++; // Advance the ball to the right + } + +// mvprintw(0, 0, "Hello, world!"); //Print some words in the spot 0,0 +// refresh(); // refresh window so words appear + +// sleep(1); //derp +// endwin(); // Restore normal terminal behavior +} + +void draw_borders(WINDOW *screen) { + int x, y, i; + + getmaxyx(screen, y, x); + + // 4 corners + mvwprintw(screen, 0, 0, "+"); + mvwprintw(screen, y - 1, 0, "+"); + mvwprintw(screen, 0, x - 1, "+"); + mvwprintw(screen, y - 1, x - 1, "+"); + + // sides + for (i = 1; i < (y - 1); i++) { + mvwprintw(screen, i, 0, "|"); + mvwprintw(screen, i, x - 1, "|"); + } + + // top and bottom + for (i = 1; i < (x - 1); i++) { + mvwprintw(screen, 0, i, "-"); + mvwprintw(screen, y - 1, i, "-"); + } +} + +void quit(){ + endwin(); + exit(0); +} diff --git a/curses/example_1/test/Makefile b/curses/example_1/test/Makefile new file mode 100644 index 0000000..df18b6e --- /dev/null +++ b/curses/example_1/test/Makefile @@ -0,0 +1,3 @@ +LDFLAGS=-lncurses + +all: demo diff --git a/curses/example_1/test/demo b/curses/example_1/test/demo new file mode 100755 index 0000000..fa442a8 Binary files /dev/null and b/curses/example_1/test/demo differ diff --git a/curses/example_1/test/demo.c b/curses/example_1/test/demo.c new file mode 100644 index 0000000..f76530e --- /dev/null +++ b/curses/example_1/test/demo.c @@ -0,0 +1,78 @@ +#include +#include + +void draw_borders(WINDOW *screen) { + int x, y, i; + + getmaxyx(screen, y, x); + + // 4 corners + mvwprintw(screen, 0, 0, "+"); + mvwprintw(screen, y - 1, 0, "+"); + mvwprintw(screen, 0, x - 1, "+"); + mvwprintw(screen, y - 1, x - 1, "+"); + + // sides + for (i = 1; i < (y - 1); i++) { + mvwprintw(screen, i, 0, "|"); + mvwprintw(screen, i, x - 1, "|"); + } + + // top and bottom + for (i = 1; i < (x - 1); i++) { + mvwprintw(screen, 0, i, "-"); + mvwprintw(screen, y - 1, i, "-"); + } +} + +int main(int argc, char *argv[]) { + int parent_x, parent_y, new_x, new_y; + int score_size = 3; + + initscr(); + noecho(); + curs_set(FALSE); + + // set up initial windows + getmaxyx(stdscr, parent_y, parent_x); + + WINDOW *field = newwin(parent_y - score_size, parent_x, 0, 0); + WINDOW *score = newwin(score_size, parent_x, parent_y - score_size, 0); + + draw_borders(field); + draw_borders(score); + + while(1) { + getmaxyx(stdscr, new_y, new_x); + + if (new_y != parent_y || new_x != parent_x) { + parent_x = new_x; + parent_y = new_y; + + wresize(field, new_y - score_size, new_x); + wresize(score, score_size, new_x); + mvwin(score, new_y - score_size, 0); + + wclear(stdscr); + wclear(field); + wclear(score); + + draw_borders(field); + draw_borders(score); + } + + // draw to our windows + mvwprintw(field, 1, 1, "Field"); + mvwprintw(score, 1, 1, "Score"); + + // refresh each window + wrefresh(field); + wrefresh(score); + } + + endwin(); + + return 0; +} + + diff --git a/curses/fun/Makefile b/curses/fun/Makefile new file mode 100644 index 0000000..837dc83 --- /dev/null +++ b/curses/fun/Makefile @@ -0,0 +1,3 @@ +LDFLAGS=-lncurses + +all: fun diff --git a/curses/fun/fun b/curses/fun/fun new file mode 100755 index 0000000..bd98e5f Binary files /dev/null and b/curses/fun/fun differ diff --git a/curses/fun/fun.c b/curses/fun/fun.c new file mode 100644 index 0000000..eca85a0 --- /dev/null +++ b/curses/fun/fun.c @@ -0,0 +1,134 @@ +#include +#include +#include + +void quit(); +void draw_borders(WINDOW *screen); +int move_player(int keypress, int* player_y, int* player_x); + +void refresh_window(WINDOW *screen); + + +int main(int argc, char *argv[]){ + int max_y, max_x, new_y, new_x, field_max_x, field_max_y, player_x, player_y; + int next_x = 0; + int keypress; + int control_size = 5; + + initscr(); // Initialize window + noecho(); // Don't echo keypresses + curs_set(FALSE); // No cursor + nodelay(stdscr,1); // Stop getch from waiting. + + getmaxyx(stdscr, max_y, max_x); //Get the max x/y of the screen + + + WINDOW *field = newwin(max_y - control_size, max_x, 0, 0); + WINDOW *controls = newwin(control_size, max_x, max_y - control_size, 0); + + draw_borders(field); + draw_borders(controls); + + + //center player on field + getmaxyx(field, field_max_y, field_max_x); + + player_x = field_max_x/2; + player_y = field_max_y/2; + + while(1){ + getmaxyx(stdscr, new_y, new_x); + + keypress = getch(); // Listen for commands; must be before window refresh + + + if (new_y != max_y || new_x != max_x) { + max_x = new_x; + max_y = new_y; + + wresize(field, new_y - control_size, new_x); + wresize(controls, control_size, new_x); + mvwin(controls, new_y - control_size, 0); + wclear(stdscr); + wclear(field); + wclear(controls); + + draw_borders(field); + draw_borders(controls); + wrefresh(stdscr); + } + + mvwprintw(field, 1, 1, "Field"); + mvwprintw(field, player_y, player_x, "o"); + + mvwprintw(controls, 1, 1, "Commands"); + mvwprintw(controls, 3, 1, "^Q to quit"); + + wrefresh(field); + wrefresh(controls); + + if (keypress != -1) { + move_player(keypress, &player_y, &player_x); + refresh_window(field); + } + } + endwin(); + + return 0; +} + +int move_player(int keypress, int* player_y, int* player_x){ + if (keypress == 'w'){ + *player_y = *player_y - 1; + } + + else if (keypress == 'a'){ + *player_x = *player_x - 1; + } + else if (keypress == 's'){ + *player_y = *player_y + 1; + } + else if (keypress == 'd'){ + *player_x = *player_x + 1; + } + + else if (keypress == 17) { + quit(); + } + +} + +void refresh_window(WINDOW *screen) { + wclear(screen); + draw_borders(screen); + wrefresh(screen); +} + +void draw_borders(WINDOW *screen) { + int x, y, i; + + getmaxyx(screen, y, x); + + // 4 corners + mvwprintw(screen, 0, 0, "+"); + mvwprintw(screen, y - 1, 0, "+"); + mvwprintw(screen, 0, x - 1, "+"); + mvwprintw(screen, y - 1, x - 1, "+"); + + // sides + for (i = 1; i < (y - 1); i++) { + mvwprintw(screen, i, 0, "|"); + mvwprintw(screen, i, x - 1, "|"); + } + + // top and bottom + for (i = 1; i < (x - 1); i++) { + mvwprintw(screen, 0, i, "-"); + mvwprintw(screen, y - 1, i, "-"); + } +} + +void quit(){ + endwin(); + exit(0); +} diff --git a/lazyfoodSDL/01_hello_SDL/01_hello_SDL b/lazyfoodSDL/01_hello_SDL/01_hello_SDL new file mode 100755 index 0000000..6fe2ca0 Binary files /dev/null and b/lazyfoodSDL/01_hello_SDL/01_hello_SDL differ diff --git a/lazyfoodSDL/01_hello_SDL/01_hello_SDL.cpp b/lazyfoodSDL/01_hello_SDL/01_hello_SDL.cpp new file mode 100644 index 0000000..2941de5 --- /dev/null +++ b/lazyfoodSDL/01_hello_SDL/01_hello_SDL.cpp @@ -0,0 +1,56 @@ +/*This source code copyrighted by Lazy Foo' Productions (2004-2015) +and may not be redistributed without written permission.*/ + +//Using SDL and standard IO +#include +#include + +//Screen dimension constants +const int SCREEN_WIDTH = 640; +const int SCREEN_HEIGHT = 480; + +int main( int argc, char* args[] ) +{ + //The window we'll be rendering to + SDL_Window* window = NULL; + + //The surface contained by the window + SDL_Surface* screenSurface = NULL; + + //Initialize SDL + if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) + { + printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() ); + } + else + { + //Create window + window = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN ); + if( window == NULL ) + { + printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() ); + } + else + { + //Get window surface + screenSurface = SDL_GetWindowSurface( window ); + + //Fill the surface white + SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0xFF, 0xFF, 0xFF ) ); + + //Update the surface + SDL_UpdateWindowSurface( window ); + + //Wait two seconds + SDL_Delay( 2000 ); + } + } + + //Destroy window + SDL_DestroyWindow( window ); + + //Quit SDL subsystems + SDL_Quit(); + + return 0; +} diff --git a/lazyfoodSDL/01_hello_SDL/01_hello_SDL.zip b/lazyfoodSDL/01_hello_SDL/01_hello_SDL.zip new file mode 100644 index 0000000..72c0bde Binary files /dev/null and b/lazyfoodSDL/01_hello_SDL/01_hello_SDL.zip differ diff --git a/lazyfoodSDL/01_hello_SDL/Makefile b/lazyfoodSDL/01_hello_SDL/Makefile new file mode 100644 index 0000000..74d349b --- /dev/null +++ b/lazyfoodSDL/01_hello_SDL/Makefile @@ -0,0 +1,20 @@ +#OBJS specifies which files to compile as part of the project +OBJS = 01_hello_SDL.cpp + +#CC specifies which compiler to use +CC = clang + +#COMPILER_FLAGS specifies the additional compilation options we're using +# -w suppress all warnings +COMPILER_FLAGS = -w + +#LINKER_FLAGS specifies the libraries we're linking against +LINKER_FLAGS = -lSDL2 + +#OBJ_NAME specifies the name of our executable +OBJ_NAME= 01_hello_SDL + +#This is the target that compiles our executable +all : $(OBJS) + $(CC) $(OBJS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME) + diff --git a/lazyfoodSDL/02_image/02_image.cpp b/lazyfoodSDL/02_image/02_image.cpp new file mode 100644 index 0000000..ca46b93 --- /dev/null +++ b/lazyfoodSDL/02_image/02_image.cpp @@ -0,0 +1,117 @@ +/*This source code copyrighted by Lazy Foo' Productions (2004-2015) +and may not be redistributed without written permission.*/ + +//Using SDL and standard IO +#include +#include + +//Screen dimensions +const int SCREEN_WIDTH = 640; +const int SCREEN_HEIGHT = 480; + +//Starts up SDL and creates window +bool init(); + +//Loads media +bool loadMedia(); + +//Frees media and shuts down SDL +void close(); + +//The window we'll be rendering to +SDL_Window* gWindow = NULL; + +//The surface contained by the window +SDL_Surface* gScreenSurface = NULL; + +//The image we will load and show on the screen +SDL_Surface* gHelloWorld = NULL; + +bool init(){ + //Initialization flag + bool success = true; + + //Initialize SDL + if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) + { + printf("SDL could not init! SDL_Error: %s\n", SDL_GetError() ); + success = false; + } + else + { + //Create window + gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN ); + if( gWindow == NULL ) + { + printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() ); + success = false; + } + else + { + //Get window surface + gScreenSurface = SDL_GetWindowSurface( gWindow ); + } + } + return success; +} + + +bool loadMedia() +{ + //Loading success flag + bool success = true; + + //Load splash image + gHelloWorld = SDL_LoadBMP( "images/hello_world.bmp" ); + if( gHelloWorld == NULL ) + { + printf( "Unable to load image %s! SDL Error: %s\n", "images/hello_world.bmp", SDL_GetError() ); + } + return success; +} + +void close() +{ + //Deallocate surface + SDL_FreeSurface( gHelloWorld ); + gHelloWorld = NULL; + + //Destroy window + SDL_DestroyWindow( gWindow ); + gWindow = NULL; + + //Quit SDL subsystems + SDL_Quit(); +} + +int main( int argc, char* args[] ) +{ + //Start up SDL and create window + if( !init() ) + { + printf( "Failed to init!\n" ); + } + else + { + //Load media + if( !loadMedia() ) + { + printf( "Failed to load media!\n"); + } + else + { + //Apply the image + SDL_BlitSurface( gHelloWorld, NULL, gScreenSurface, NULL ); + + //Update the surface + SDL_UpdateWindowSurface( gWindow ); + + //Wait two seconds + SDL_Delay( 2000 ); + } + } + //Free resources and close SDL + close(); + + return 0; +} diff --git a/lazyfoodSDL/02_image/Makefile b/lazyfoodSDL/02_image/Makefile new file mode 100644 index 0000000..443f122 --- /dev/null +++ b/lazyfoodSDL/02_image/Makefile @@ -0,0 +1,20 @@ +#OBJS specifies which files to compile as part of the project +OBJS = 02_image.cpp + +#CC specifies which compiler to use +CC = clang + +#COMPILER_FLAGS specifies the additional compilation options we're using +# -w suppress all warnings +COMPILER_FLAGS = -w + +#LINKER_FLAGS specifies the libraries we're linking against +LINKER_FLAGS = -lSDL2 + +#OBJ_NAME specifies the name of our executable +OBJ_NAME= 02_image + +#This is the target that compiles our executable +all : $(OBJS) + $(CC) $(OBJS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME) + diff --git a/lazyfoodSDL/02_image/images/hello_world.bmp b/lazyfoodSDL/02_image/images/hello_world.bmp new file mode 100755 index 0000000..def2531 Binary files /dev/null and b/lazyfoodSDL/02_image/images/hello_world.bmp differ diff --git a/lazyfoodSDL/04_keypress/04_keepress b/lazyfoodSDL/04_keypress/04_keepress new file mode 100755 index 0000000..3ed1799 Binary files /dev/null and b/lazyfoodSDL/04_keypress/04_keepress differ diff --git a/lazyfoodSDL/04_keypress/Makefile b/lazyfoodSDL/04_keypress/Makefile new file mode 100644 index 0000000..b694a4f --- /dev/null +++ b/lazyfoodSDL/04_keypress/Makefile @@ -0,0 +1,20 @@ +#OBJS specifies which files to compile as part of the project +OBJS = test.cpp + +#CC specifies which compiler to use +CC = g++ + +#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 = -lSDL2 + +#OBJ_NAME specifies the name of our executable +OBJ_NAME= 04_keepress + +#This is the target that compiles our executable +all : $(OBJS) + $(CC) $(OBJS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME) + diff --git a/lazyfoodSDL/04_keypress/images/down.bmp b/lazyfoodSDL/04_keypress/images/down.bmp new file mode 100755 index 0000000..da01d0d Binary files /dev/null and b/lazyfoodSDL/04_keypress/images/down.bmp differ diff --git a/lazyfoodSDL/04_keypress/images/hello_world.bmp b/lazyfoodSDL/04_keypress/images/hello_world.bmp new file mode 100755 index 0000000..def2531 Binary files /dev/null and b/lazyfoodSDL/04_keypress/images/hello_world.bmp differ diff --git a/lazyfoodSDL/04_keypress/images/left.bmp b/lazyfoodSDL/04_keypress/images/left.bmp new file mode 100755 index 0000000..8f4136c Binary files /dev/null and b/lazyfoodSDL/04_keypress/images/left.bmp differ diff --git a/lazyfoodSDL/04_keypress/images/press.bmp b/lazyfoodSDL/04_keypress/images/press.bmp new file mode 100755 index 0000000..3d9e142 Binary files /dev/null and b/lazyfoodSDL/04_keypress/images/press.bmp differ diff --git a/lazyfoodSDL/04_keypress/images/right.bmp b/lazyfoodSDL/04_keypress/images/right.bmp new file mode 100755 index 0000000..e52d082 Binary files /dev/null and b/lazyfoodSDL/04_keypress/images/right.bmp differ diff --git a/lazyfoodSDL/04_keypress/images/up.bmp b/lazyfoodSDL/04_keypress/images/up.bmp new file mode 100755 index 0000000..a8e5efa Binary files /dev/null and b/lazyfoodSDL/04_keypress/images/up.bmp differ diff --git a/lazyfoodSDL/04_keypress/images/x.bmp b/lazyfoodSDL/04_keypress/images/x.bmp new file mode 100755 index 0000000..9633a7e Binary files /dev/null and b/lazyfoodSDL/04_keypress/images/x.bmp differ diff --git a/lazyfoodSDL/04_keypress/test.cpp b/lazyfoodSDL/04_keypress/test.cpp new file mode 100644 index 0000000..c60ca96 --- /dev/null +++ b/lazyfoodSDL/04_keypress/test.cpp @@ -0,0 +1,230 @@ +/*This source code copyrighted by Lazy Foo' Productions (2004-2015) +and may not be redistributed without written permission.*/ + +//Using SDL and standard IO +#include +#include +#include + +//Screen dimensions +const int SCREEN_WIDTH = 640; +const int SCREEN_HEIGHT = 480; + +//Key press surfaces constants +//enum makes everything a const int +//and sets it to numbers sequentaly starting with 0 +enum KeyPressSurfaces +{ + KEY_PRESS_SURFACE_DEFAULT, + KEY_PRESS_SURFACE_UP, + KEY_PRESS_SURFACE_DOWN, + KEY_PRESS_SURFACE_LEFT, + KEY_PRESS_SURFACE_RIGHT, + KEY_PRESS_SURFACE_TOTAL +}; + +//Starts up SDL and creates window +bool init(); + +//Loads media +bool loadMedia(); + +//Frees media and shuts down SDL +void close(); + +//Loads individual image +SDL_Surface* loadSurface( std::string path ); + +//The window we'll be rendering to +SDL_Window* gWindow = NULL; + +//The surface contained by the window +SDL_Surface* gScreenSurface = NULL; + +//The images that correspond to a keypress +SDL_Surface* gKeyPressSurfaces[ KEY_PRESS_SURFACE_TOTAL ]; + +//Current displayed image +SDL_Surface* gCurrentSurface = NULL; + +SDL_Surface* loadSurface( std::string path ) +{ + //Load image at specified path + SDL_Surface* loadedSurface = SDL_LoadBMP( path.c_str() ); + if( loadedSurface == NULL ) + { + printf( "Unable to load image %s! SDL Error: %s\n", path.c_str(), SDL_GetError() ); + } + + return loadedSurface; +} + +bool init(){ + //Initialization flag + bool success = true; + + //Initialize SDL + if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) + { + printf("SDL could not init! SDL_Error: %s\n", SDL_GetError() ); + success = false; + } + else + { + //Create window + gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN ); + if( gWindow == NULL ) + { + printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() ); + success = false; + } + else + { + //Get window surface + gScreenSurface = SDL_GetWindowSurface( gWindow ); + } + } + return success; +} + + +bool loadMedia() +{ + //Loading success flag + bool success = true; + + //Load default surface + gKeyPressSurfaces[ KEY_PRESS_SURFACE_DEFAULT ] = SDL_LoadBMP( "images/press.bmp" ); + if( gKeyPressSurfaces[ KEY_PRESS_SURFACE_DEFAULT ] == NULL ) + { + printf( "Failed to load default image!\n" ); + success = false; + } + + gKeyPressSurfaces[ KEY_PRESS_SURFACE_UP] = SDL_LoadBMP( "images/up.bmp" ); + if( gKeyPressSurfaces[ KEY_PRESS_SURFACE_UP ] == NULL ) + { + printf( "Failed to load up image!\n" ); + success = false; + } + + gKeyPressSurfaces[ KEY_PRESS_SURFACE_DOWN] = loadSurface( "images/down.bmp" ); + if( gKeyPressSurfaces[ KEY_PRESS_SURFACE_DOWN ] == NULL ) + { + printf( "Failed to load down image!\n" ); + success = false; + } + + gKeyPressSurfaces[ KEY_PRESS_SURFACE_LEFT] = loadSurface( "images/left.bmp" ); + if( gKeyPressSurfaces[ KEY_PRESS_SURFACE_LEFT ] == NULL ) + { + printf( "Failed to load left image!\n" ); + success = false; + } + + gKeyPressSurfaces[ KEY_PRESS_SURFACE_RIGHT] = loadSurface( "images/right.bmp" ); + if( gKeyPressSurfaces[ KEY_PRESS_SURFACE_RIGHT ] == NULL ) + { + printf( "Failed to load right image!\n" ); + success = false; + } + + + return success; +} + +void close() +{ + //Deallocate surface + for ( int i=0; i < KEY_PRESS_SURFACE_TOTAL; ++i ) + { + SDL_FreeSurface( gKeyPressSurfaces[ i ] ); + gKeyPressSurfaces[ i ] = NULL; + } + + //Destroy window + SDL_DestroyWindow( gWindow ); + gWindow = NULL; + + //Quit SDL subsystems + SDL_Quit(); +} + +int main( int argc, char* args[] ) +{ + //Start up SDL and create window + if( !init() ) + { + printf( "Failed to init!\n" ); + } + else + { + //Load media + if( !loadMedia() ) + { + printf( "Failed to load media!\n"); + } + else + { + //Main loop flag + bool quit = false; + + //Event handler + SDL_Event e; + + //Set default current surface + gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_DEFAULT ]; + + //While application is running + while( !quit ) + { + //Handle events on queue + while( SDL_PollEvent( &e ) !=0 ) + { + //User requests quit + if( e.type == SDL_QUIT ) + { + quit = true; + } + //User presses a key + else if( e.type == SDL_KEYDOWN ) + { + //Select surfaces based on key press + switch( e.key.keysym.sym ) + { + case SDLK_UP: + gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_UP ]; + break; + + case SDLK_DOWN: + gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_DOWN ]; + break; + + case SDLK_LEFT: + gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_LEFT ]; + break; + + case SDLK_RIGHT: + gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_RIGHT ]; + break; + + default: + gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_DEFAULT ]; + break; + } + } + } + + //Apply the current image + SDL_BlitSurface( gCurrentSurface, NULL, gScreenSurface, NULL ); + + //Update the surface + SDL_UpdateWindowSurface( gWindow ); + } + } + } + //Free resources and close SDL + close(); + + return 0; +} diff --git a/learncthehardway/3/Makefile b/learncthehardway/3/Makefile new file mode 100644 index 0000000..b48e222 --- /dev/null +++ b/learncthehardway/3/Makefile @@ -0,0 +1,4 @@ +CFLAGS=-Wall -g + +clean: + rm -f ex1 diff --git a/learncthehardway/3/ex3 b/learncthehardway/3/ex3 new file mode 100755 index 0000000..84bab2e Binary files /dev/null and b/learncthehardway/3/ex3 differ diff --git a/learncthehardway/3/ex3.c b/learncthehardway/3/ex3.c new file mode 100644 index 0000000..35e0486 --- /dev/null +++ b/learncthehardway/3/ex3.c @@ -0,0 +1,12 @@ +#include + +int main() +{ + int age = 10; + int height = 72; + + printf("I am %d years old.\n", age ); + printf("I am %d inches tall.\n", height); + + return 0; +} diff --git a/learncthehardway/4/Makefile b/learncthehardway/4/Makefile new file mode 100644 index 0000000..5fa7082 --- /dev/null +++ b/learncthehardway/4/Makefile @@ -0,0 +1,4 @@ +CFLAGS=-Wall -g + +clean: + rm -f ex4 diff --git a/learncthehardway/4/ex4 b/learncthehardway/4/ex4 new file mode 100755 index 0000000..ef480ee Binary files /dev/null and b/learncthehardway/4/ex4 differ diff --git a/learncthehardway/4/ex4.c b/learncthehardway/4/ex4.c new file mode 100644 index 0000000..ec31fa9 --- /dev/null +++ b/learncthehardway/4/ex4.c @@ -0,0 +1,14 @@ +#include + +/* Warning: This program is wrong on purpose. */ + +int main() +{ + int age = 10; + int height; + + printf("I am %d years old.\n"); + printf("I am %d inches tall.\n", height); + + return 0; +} diff --git a/learncthehardway/ex10/a.out b/learncthehardway/ex10/a.out new file mode 100755 index 0000000..828c1f0 Binary files /dev/null and b/learncthehardway/ex10/a.out differ diff --git a/learncthehardway/ex10/ex10.c b/learncthehardway/ex10/ex10.c new file mode 100644 index 0000000..6e709a5 --- /dev/null +++ b/learncthehardway/ex10/ex10.c @@ -0,0 +1,23 @@ +#include + +int main(int argc, char *argv[]) +{ + int i = 0; + + // go through each string in argv + for (i = 1; i < argc; i++) { + printf("arg %d: %s\n", i, argv[i]); + } + + // Make an array of strings + char *states[] = { + "California", "Oregon", + "Washington", "Texas" + }; + int num_states = 4; + + for (i = 0; i < num_states; i++) { + printf("states %d: %s\n", i, states[i]); + } + return 0; +} diff --git a/learncthehardway/ex14/a.out b/learncthehardway/ex14/a.out new file mode 100755 index 0000000..b415a06 Binary files /dev/null and b/learncthehardway/ex14/a.out differ diff --git a/learncthehardway/ex14/ex14.c b/learncthehardway/ex14/ex14.c new file mode 100644 index 0000000..395383e --- /dev/null +++ b/learncthehardway/ex14/ex14.c @@ -0,0 +1,42 @@ +#include +#include + +// forward declarations +int can_print_it(char ch); +void print_letters(char arg[]); + +void print_arguments(int argc, char *argv[]) +{ + int i = 0; + + for(i = 0; i < argc; i++) { + print_letters(argv[i]); + } +} + +void print_letters(char arg[]) +{ + int i = 0; + + for(i = 0; arg[i] != '\0'; i++) { + char ch = arg[i]; + + if(can_print_it(ch)) { + printf("'%c' == %d ", ch, ch); + } + } + + printf("\n"); +} + +int can_print_it(char ch) +{ + return isalpha(ch) || isblank(ch); +} + + +int main(int argc, char *argv[]) +{ + print_arguments(argc, argv); + return 0; +} diff --git a/learncthehardway/ex9/notes b/learncthehardway/ex9/notes new file mode 100644 index 0000000..f20f9e8 --- /dev/null +++ b/learncthehardway/ex9/notes @@ -0,0 +1,5 @@ +instead of doing something like + +char name[4] = "zed" +do +char *name = "zed" diff --git a/learncthehardway/exc1/Makefile b/learncthehardway/exc1/Makefile new file mode 100644 index 0000000..1a973e2 --- /dev/null +++ b/learncthehardway/exc1/Makefile @@ -0,0 +1,6 @@ +CFLAGS=-Wall -g + +clean: + rm -f ex1 +all: + ex1 diff --git a/learncthehardway/exc1/a.out b/learncthehardway/exc1/a.out new file mode 100755 index 0000000..8d9ab93 Binary files /dev/null and b/learncthehardway/exc1/a.out differ diff --git a/learncthehardway/exc1/ex1.c b/learncthehardway/exc1/ex1.c new file mode 100644 index 0000000..3785f1a --- /dev/null +++ b/learncthehardway/exc1/ex1.c @@ -0,0 +1,8 @@ +#include + +int main(int argc, char *argv[]) +{ + puts("Hello world."); + + return 0; +} diff --git a/learncthehardway/exc2/Makefile b/learncthehardway/exc2/Makefile new file mode 100644 index 0000000..b48e222 --- /dev/null +++ b/learncthehardway/exc2/Makefile @@ -0,0 +1,4 @@ +CFLAGS=-Wall -g + +clean: + rm -f ex1 diff --git a/sdl/first/Makefile b/sdl/first/Makefile new file mode 100644 index 0000000..fff491d --- /dev/null +++ b/sdl/first/Makefile @@ -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 diff --git a/sdl/first/bat.bmp b/sdl/first/bat.bmp new file mode 100644 index 0000000..6931089 Binary files /dev/null and b/sdl/first/bat.bmp differ diff --git a/sdl/first/first b/sdl/first/first new file mode 100755 index 0000000..c90031f Binary files /dev/null and b/sdl/first/first differ diff --git a/sdl/first/main.c b/sdl/first/main.c new file mode 100644 index 0000000..6f99b1b --- /dev/null +++ b/sdl/first/main.c @@ -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); +} diff --git a/structus/Makefile b/structus/Makefile new file mode 100644 index 0000000..7a81d36 --- /dev/null +++ b/structus/Makefile @@ -0,0 +1,22 @@ +#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 = + +#OBJ_NAME specifies the name of our executable +OBJ_NAME= struct + +#This is the target that compiles our executable +all : $(OBJS) + $(CC) $(OBJS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME) +clean : + rm struct + diff --git a/structus/main.c b/structus/main.c new file mode 100644 index 0000000..35f3f8a --- /dev/null +++ b/structus/main.c @@ -0,0 +1,43 @@ +#include + +int gain(int current, int amount_gained); + +void gain_by_pointer(int *current, int amount_gained); + +int main(){ + struct players{ + int life; + int magic; + }; + + struct players player1; + player1.life = 5; + player1.magic = 0; + printf(" Hi\n player1's life is currently: %d\n", player1.life); + printf(" player1's magic is currently: %d\n", player1.magic); + + player1.magic = gain(player1.magic, 5); + player1.life = gain(player1.life, 32); + + printf("\n player1's new magic is %d: \n", player1.magic); + + printf(" player1's new life is %d: \n", player1.life); + + printf(" Adding 1 to both life and magic through pointer magic! \n"); + + gain_by_pointer(&player1.magic, 1); + gain_by_pointer(&player1.life, 1); + + printf("\n Thanks to pointer magic, life is now %d, magic is now, %d\n", player1.life, player1.magic); + + +} + +void gain_by_pointer(int *current, int amount_gained){ + *current += amount_gained; +} +int gain(int current, int amount_gained){ + int new_current; + new_current = current + amount_gained; + return new_current; +} diff --git a/structus/struct b/structus/struct new file mode 100755 index 0000000..90bb477 Binary files /dev/null and b/structus/struct differ