init commit

This commit is contained in:
Logen Kain 2016-07-21 10:22:47 -07:00
commit cb4eb030bd
55 changed files with 1209 additions and 0 deletions

BIN
C_BOOK/1.5.1/a.out Executable file

Binary file not shown.

13
C_BOOK/1.5.1/main.c Normal file
View File

@ -0,0 +1,13 @@
#define Q 113
#include <stdio.h>
int main()
{
int c;
while (c != 'q') {
c = getchar();
printf("%d\n",c);
}
return 0;
}

2
C_BOOK/1.5.1/test.txt Normal file
View File

@ -0,0 +1,2 @@
bob is a faggot
bob is also an u.

52
C_BOOK/1.9-arrays/main.c Normal file
View File

@ -0,0 +1,52 @@
#include <stdio.h>
#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<lim-1 && (c=getchar()) !=EOF && c != '\n'; ++i)
s[i] =c;
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
/* copy: copy 'from' into 'to'; assume to is big enough */
void copy (char to[], char from[])
{
int i;
i = 0;
while ((to[i] = from[i]) != '\0')
++i;
}

BIN
c_graphics/sample/demo Executable file

Binary file not shown.

24
c_graphics/sample/demo.c Normal file
View File

@ -0,0 +1,24 @@
/* demo.c*/
#include <GL/gl.h>
#include <GL/glut.h>
#include <GL/glu.h>
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;
}

14
c_graphics/sample/test.c Normal file
View File

@ -0,0 +1,14 @@
#include<graphics.h>
#include<conio.h>
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
getch();
closegraph();
return0
}

View File

@ -0,0 +1,3 @@
LDFLAGS=-lncurses
all: demo

BIN
curses/example_1/demo Executable file

Binary file not shown.

92
curses/example_1/demo.c Normal file
View File

@ -0,0 +1,92 @@
#include <stdlib.h>
#include <ncurses.h>
#include <unistd.h>
#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);
}

View File

@ -0,0 +1,3 @@
LDFLAGS=-lncurses
all: demo

BIN
curses/example_1/test/demo Executable file

Binary file not shown.

View File

@ -0,0 +1,78 @@
#include <ncurses.h>
#include <unistd.h>
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;
}

3
curses/fun/Makefile Normal file
View File

@ -0,0 +1,3 @@
LDFLAGS=-lncurses
all: fun

BIN
curses/fun/fun Executable file

Binary file not shown.

134
curses/fun/fun.c Normal file
View File

@ -0,0 +1,134 @@
#include <stdlib.h>
#include <ncurses.h>
#include <unistd.h>
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);
}

Binary file not shown.

View File

@ -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 <SDL2/SDL.h>
#include <stdio.h>
//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;
}

Binary file not shown.

View File

@ -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)

View File

@ -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 <SDL2/SDL.h>
#include <stdio.h>
//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;
}

View File

@ -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)

Binary file not shown.

After

Width:  |  Height:  |  Size: 900 KiB

Binary file not shown.

View File

@ -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)

Binary file not shown.

After

Width:  |  Height:  |  Size: 900 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 900 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 900 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 900 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 900 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 900 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 900 KiB

View File

@ -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 <SDL2/SDL.h>
#include <stdio.h>
#include <string>
//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;
}

View File

@ -0,0 +1,4 @@
CFLAGS=-Wall -g
clean:
rm -f ex1

BIN
learncthehardway/3/ex3 Executable file

Binary file not shown.

12
learncthehardway/3/ex3.c Normal file
View File

@ -0,0 +1,12 @@
#include <stdio.h>
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;
}

View File

@ -0,0 +1,4 @@
CFLAGS=-Wall -g
clean:
rm -f ex4

BIN
learncthehardway/4/ex4 Executable file

Binary file not shown.

14
learncthehardway/4/ex4.c Normal file
View File

@ -0,0 +1,14 @@
#include <stdio.h>
/* 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;
}

BIN
learncthehardway/ex10/a.out Executable file

Binary file not shown.

View File

@ -0,0 +1,23 @@
#include <stdio.h>
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;
}

BIN
learncthehardway/ex14/a.out Executable file

Binary file not shown.

View File

@ -0,0 +1,42 @@
#include <stdio.h>
#include <ctype.h>
// 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;
}

View File

@ -0,0 +1,5 @@
instead of doing something like
char name[4] = "zed"
do
char *name = "zed"

View File

@ -0,0 +1,6 @@
CFLAGS=-Wall -g
clean:
rm -f ex1
all:
ex1

BIN
learncthehardway/exc1/a.out Executable file

Binary file not shown.

View File

@ -0,0 +1,8 @@
#include <stdio.h>
int main(int argc, char *argv[])
{
puts("Hello world.");
return 0;
}

View File

@ -0,0 +1,4 @@
CFLAGS=-Wall -g
clean:
rm -f ex1

25
sdl/first/Makefile Normal file
View 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

Binary file not shown.

After

Width:  |  Height:  |  Size: 251 KiB

BIN
sdl/first/first Executable file

Binary file not shown.

116
sdl/first/main.c Normal file
View 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);
}

22
structus/Makefile Normal file
View File

@ -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

43
structus/main.c Normal file
View File

@ -0,0 +1,43 @@
#include <stdio.h>
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;
}

BIN
structus/struct Executable file

Binary file not shown.