|
| 1 | +/*My very first graphics programming code |
| 2 | + *My first program using APIs |
| 3 | + *I used SDL 2.0.4 at the time of this creation (09/21/16) |
| 4 | + */ |
| 5 | + |
| 6 | +/* |
| 7 | + * This program makes use of rendering |
| 8 | + * objects to the screen frame by frame; |
| 9 | + * wihch is basically animation |
| 10 | + */ |
| 11 | + |
| 12 | +/* |
| 13 | + * CONTROLS: |
| 14 | + * Up Arrow key - moves object up |
| 15 | + * Down Arrow key - moves object down |
| 16 | + * Left Arrow key - moves object to the left |
| 17 | + * Right Arrow key - moves object to the right |
| 18 | + * Enter key - enlarges the object |
| 19 | + * Backspace key - reduces object size |
| 20 | + * |
| 21 | + */ |
| 22 | + |
| 23 | +#include<stdio.h> |
| 24 | +#include<stdlib.h> |
| 25 | +#include"SDL.h" |
| 26 | + |
| 27 | +const unsigned int WIDTH = 640; |
| 28 | +const unsigned int HEIGHT = 480; |
| 29 | + |
| 30 | + |
| 31 | +typedef struct object { |
| 32 | + unsigned int size; //size of object |
| 33 | + int x; //x-coordinate of object |
| 34 | + int y; //y-coordinate of object |
| 35 | +} Object; |
| 36 | + |
| 37 | +void init(SDL_Window **window, SDL_Renderer **renderer) { |
| 38 | + |
| 39 | + SDL_Init(SDL_INIT_VIDEO); |
| 40 | + |
| 41 | + *window = SDL_CreateWindow("Motion Rendering", |
| 42 | + SDL_WINDOWPOS_UNDEFINED, |
| 43 | + SDL_WINDOWPOS_UNDEFINED, |
| 44 | + WIDTH, |
| 45 | + HEIGHT, |
| 46 | + 0); |
| 47 | + |
| 48 | + if(*window == NULL) { |
| 49 | + printf("ERROR; Window cannot be initialized.\n"); |
| 50 | + exit(EXIT_FAILURE); |
| 51 | + } |
| 52 | + |
| 53 | + *renderer = SDL_CreateRenderer(*window, -1, |
| 54 | + SDL_RENDERER_ACCELERATED | |
| 55 | + SDL_RENDERER_PRESENTVSYNC); |
| 56 | + |
| 57 | + if(*renderer == NULL) { |
| 58 | + printf("ERROR: Renderer could not be initialized.\n"); |
| 59 | + exit(EXIT_FAILURE); |
| 60 | + } |
| 61 | + |
| 62 | +} |
| 63 | +void destroy(SDL_Window **window, SDL_Renderer **renderer) { |
| 64 | + |
| 65 | + //destroy objects |
| 66 | + SDL_DestroyWindow(*window); |
| 67 | + SDL_DestroyRenderer(*renderer); |
| 68 | + |
| 69 | +} |
| 70 | +int processEvents(SDL_Event *event, Object *object) { |
| 71 | + |
| 72 | + while(SDL_PollEvent(event)) |
| 73 | + { |
| 74 | + |
| 75 | + switch(event->type) |
| 76 | + { |
| 77 | + case SDL_QUIT: |
| 78 | + return 1; |
| 79 | + case SDL_WINDOWEVENT_CLOSE: |
| 80 | + return 1; |
| 81 | + |
| 82 | + case SDL_KEYDOWN: |
| 83 | + switch(event->key.keysym.sym) |
| 84 | + { |
| 85 | + case SDLK_ESCAPE: |
| 86 | + case SDL_QUIT: |
| 87 | + return 1; |
| 88 | + case SDLK_UP: |
| 89 | + object->y -= 30; |
| 90 | + break; |
| 91 | + case SDLK_DOWN: |
| 92 | + object->y += 30; |
| 93 | + break; |
| 94 | + case SDLK_LEFT: |
| 95 | + object->x -= 30; |
| 96 | + break; |
| 97 | + case SDLK_RIGHT: |
| 98 | + object->x += 30; |
| 99 | + break; |
| 100 | + case SDLK_RETURN: |
| 101 | + object->size += 10; |
| 102 | + object->x -= 5; |
| 103 | + object->y -= 5; |
| 104 | + break; |
| 105 | + case SDLK_BACKSPACE: |
| 106 | + object->size -= 10; |
| 107 | + object->x += 5; |
| 108 | + object->y += 5; |
| 109 | + break; |
| 110 | + } |
| 111 | + |
| 112 | + printf("Object location: [%i, %i]\tsize = %i\n", object->x, object->y, object->size); |
| 113 | + break; |
| 114 | + } |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + return 0; |
| 119 | + |
| 120 | +} |
| 121 | +void renderOnScreen(SDL_Window **window, SDL_Renderer **renderer, const Object *object) { |
| 122 | + |
| 123 | + //set render color to blue |
| 124 | + SDL_SetRenderDrawColor(*renderer, 0, 0, 255, 255); |
| 125 | + //colors the active renderer to blue |
| 126 | + SDL_RenderClear(*renderer); |
| 127 | + //sets render color to white |
| 128 | + SDL_SetRenderDrawColor(*renderer, 255, 255, 255, 255); |
| 129 | + |
| 130 | + //Declare & initialize rect. object |
| 131 | + SDL_Rect rect = {object->x, object->y, object->size, object->size}; |
| 132 | + |
| 133 | + //fill to the current rendering target |
| 134 | + SDL_RenderFillRect(*renderer, &rect); |
| 135 | + |
| 136 | + //present the drawing on the renderer to the screen |
| 137 | + SDL_RenderPresent(*renderer); |
| 138 | + |
| 139 | + //to avoid CPU overheating |
| 140 | + SDL_Delay(10); |
| 141 | + |
| 142 | +} |
| 143 | + |
| 144 | +int main(int argc, char *argv[]) { |
| 145 | + |
| 146 | + SDL_Window *window = NULL; |
| 147 | + SDL_Renderer *renderer = NULL; |
| 148 | + SDL_Event event; |
| 149 | + |
| 150 | + Object object = {200, WIDTH/2 - object.size / 2, HEIGHT/2 - object.size / 2}; |
| 151 | + int done = 0; |
| 152 | + |
| 153 | + //initialize all variables needed |
| 154 | + init(&window, &renderer); |
| 155 | + |
| 156 | + while(!done) { |
| 157 | + done = processEvents(&event, &object); |
| 158 | + renderOnScreen(&window, &renderer, &object); |
| 159 | + } |
| 160 | + |
| 161 | + destroy(&window, &renderer); |
| 162 | + |
| 163 | + //clean-up and quit |
| 164 | + SDL_Quit(); |
| 165 | + |
| 166 | + return EXIT_SUCCESS; |
| 167 | + |
| 168 | +} |
0 commit comments