Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <SDL2/SDL.h>
- #include <SDL2/SDL_timer.h>
- #include <SDL2/SDL_image.h>
- #include <math.h>
- #define WINDOW_HEIGHT (760)
- #define WINDOW_WIDTH (1024)
- //pixel / second
- #define SPEED (600)
- #define SHAR_SPD (450)
- void check(float *x_vel, float *y_vel, float *x, float *y, float sh_w, float sh_h, SDL_Rect *all_blcks, int nmb_blcks) {
- int i;
- if (*x < 0) {
- *x_vel *= -1;
- *x = -*x;
- }
- if (*x + sh_w > WINDOW_WIDTH) {
- *x_vel *= -1;
- *x = WINDOW_WIDTH - sh_w;
- }
- if (*y < 0) {
- *y_vel *= -1;
- *y = -*y;
- }
- if (*y + sh_h > WINDOW_HEIGHT) {
- *y_vel *= -1;
- *y = WINDOW_HEIGHT - sh_h;
- }
- for (i = 0; i < nmb_blcks; i++) {
- float xb, yb, wb, hb;
- xb = all_blcks[i].x;
- yb = all_blcks[i].y;
- wb = all_blcks[i].w;
- hb = all_blcks[i].h;
- if ((*x < xb && *x + sh_w > xb) || (*x > xb && *x + sh_w < xb + wb) ||
- (*x < xb + wb && *x + sh_w > xb + wb)) {
- if ((*y + sh_h > yb - 1 && *y < yb ) || (*y < yb + hb + 1 && *y + sh_h > yb + hb)) {
- if (*y_vel > 0)
- *y = yb - sh_h;
- else
- *y = yb + hb;
- *y_vel *= -1;
- break;
- }
- } else if ((*y < yb && *y + sh_h > yb) || (*y > yb && *y + sh_h < yb + hb) ||
- (*y < yb + hb && *y + sh_h > yb + hb)) {
- if ((*x + sh_w > xb && *x < xb) || (*x < xb + wb && *x + sh_w > xb + wb)) {
- if (*x_vel > 0)
- *x = xb - sh_w;
- else
- *x = xb + wb;
- *x_vel *= -1;
- break;
- }
- }
- // if ((*x > xb) && (*x < xb + wb) && (*y > yb) && (*y < yb + hb)) {
- // float wlx, wly;
- // wlx = xb + (*x_vel - fabsf(*x_vel)) * (wb) / (*x_vel * 2);
- // wly = yb + (*y_vel - fabsf(*y_vel)) * (hb) / (*y_vel * 2);
- // if (fabsf((wlx - *x) * *y_vel) > fabsf((wly - *y) * *x_vel)) {
- // *y += (wly - *y) * 2;
- // *y_vel *= -1;
- // } else {
- // *x += (wlx - *x) * 2;
- // *x_vel *= -1;
- // }
- // }
- }
- }
- int main(int argc, char *argv[]) {
- //initialize
- if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) {
- printf("%s", SDL_GetError());
- return 1;
- }
- SDL_Window *win = SDL_CreateWindow("Hello!", SDL_WINDOWPOS_CENTERED,
- SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, 0);
- if (!win) {
- SDL_Quit();
- return 1;
- }
- //create a render
- Uint32 render_flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC;
- SDL_Renderer *rend = SDL_CreateRenderer(win, -1, render_flags);
- if (!rend) {
- SDL_DestroyWindow(win);
- SDL_Quit();
- }
- //load image into memory using SDL_image
- SDL_Surface *surface = IMG_Load("D:/CArcanoid/resources/hello1.png");
- SDL_Surface *block1 = IMG_Load("D:/CArcanoid/resources/hello2.png");
- SDL_Surface *block2 = IMG_Load("D:/CArcanoid/resources/hello3.png");
- SDL_Surface *shar = IMG_Load("D:/CArcanoid/resources/hello.png");
- if (!surface) {
- SDL_DestroyRenderer(rend);
- SDL_DestroyWindow(win);
- SDL_Quit();
- }
- //load image data imto the graphics hardware's memory
- SDL_Texture *tex = SDL_CreateTextureFromSurface(rend, surface);
- SDL_Texture *tex1 = SDL_CreateTextureFromSurface(rend, block1);
- SDL_Texture *tex2 = SDL_CreateTextureFromSurface(rend, block2);
- SDL_Texture *tex_shar = SDL_CreateTextureFromSurface(rend, shar);
- SDL_FreeSurface(surface);
- if (!tex) {
- SDL_DestroyRenderer(rend);
- SDL_DestroyWindow(win);
- SDL_Quit();
- }
- //struct to hold the pos and size of the sprite
- int nmb_blcks = 0; // колво блоков(включая платформу)
- int mx_nm_bl = 10; // макс колво блоков которое будет создано в задаче
- SDL_Rect *all_blcks = malloc(sizeof(SDL_Rect) * mx_nm_bl);
- SDL_Rect dest;
- SDL_Rect dest2;
- SDL_Rect dest1;
- SDL_Rect shr;
- nmb_blcks += 3;
- //get the dimensions of the texture
- SDL_QueryTexture(tex, NULL, NULL, &dest.w, &dest.h);
- SDL_QueryTexture(tex1, NULL, NULL, &dest1.w, &dest1.h);
- SDL_QueryTexture(tex2, NULL, NULL, &dest2.w, &dest2.h);
- SDL_QueryTexture(tex_shar, NULL, NULL, &shr.w, &shr.h);
- dest.w /= 4;
- dest.h /= 4;
- dest1.w /= 4;
- dest1.h /= 4;
- dest2.w /= 4;
- dest2.h /= 4;
- shr.w /= 10;
- shr.h /= 10;
- all_blcks[0] = dest;
- all_blcks[1] = dest1;
- all_blcks[2] = dest2;
- //pos the sprite at the bottom center of the window
- // origin is the top left corner, positive y - down
- float x_pos = (WINDOW_WIDTH - all_blcks[0].w) / 2;
- float y_pos = (WINDOW_HEIGHT - all_blcks[0].h);
- float x_sh = (WINDOW_WIDTH - shr.w) / 2;
- float y_sh = (WINDOW_HEIGHT - shr.h) / 2;
- //initial velocities
- float x_vel = 0;
- float y_vel = 0;
- float x_sh_vel = SHAR_SPD;
- float y_sh_vel = SHAR_SPD;
- //keep track of which inputs are given
- int up = 0;
- int down = 0;
- int left = 0;
- int right = 0;
- //set to 1 when the close button is pressed
- int close_requested = 0;
- //animation loop
- int cnt = 0;
- while (!close_requested) {
- cnt++;
- //process events
- SDL_Event event;
- while (SDL_PollEvent(&event)) {
- switch (event.type) {
- case SDL_QUIT:
- close_requested = 1;
- break;
- //keyboard control
- case SDL_KEYDOWN:
- switch (event.key.keysym.scancode) {
- // case SDL_SCANCODE_W:
- // case SDL_SCANCODE_UP:
- // up = 1;
- // break;
- case SDL_SCANCODE_A:
- case SDL_SCANCODE_LEFT:
- left = 1;
- break;
- // case SDL_SCANCODE_S:
- // case SDL_SCANCODE_DOWN:
- // down = 1;
- // break;
- case SDL_SCANCODE_D:
- case SDL_SCANCODE_RIGHT:
- right = 1;
- break;
- }
- break;
- case SDL_KEYUP:
- switch (event.key.keysym.scancode) {
- // case SDL_SCANCODE_W:
- // case SDL_SCANCODE_UP:
- // up = 0;
- // break;
- case SDL_SCANCODE_A:
- case SDL_SCANCODE_LEFT:
- left = 0;
- break;
- // case SDL_SCANCODE_S:
- // case SDL_SCANCODE_DOWN:
- // down = 0;
- // break;
- case SDL_SCANCODE_D:
- case SDL_SCANCODE_RIGHT:
- right = 0;
- break;
- }
- break;
- }
- }
- x_vel = y_vel = 0;
- // if (up && !down) y_vel = -SPEED;
- // if (down && !up) y_vel = SPEED;
- if (left && !right) x_vel = -SPEED;
- if (right && !left) x_vel = SPEED;
- // int mouse_x, mouse_y;
- // int buttons = SDL_GetMouseState(&mouse_x, &mouse_y);
- //
- // int target_x = mouse_x - dest.w / 2;
- // int target_y = mouse_y - dest.h / 2;
- // float delta_x = target_x - x_pos;
- // float delta_y = target_y - y_pos;
- // float distance = sqrt(delta_x * delta_x + delta_y * delta_y);
- //
- // //prevent jitter
- // if (distance < 5)
- // x_vel = y_vel = 0;
- // else {
- // x_vel = delta_x * SPEED / distance;
- // y_vel = delta_y * SPEED / distance;
- // }
- //
- // //reverse velocity if mouse buuton 1 pressed
- // if (buttons & SDL_BUTTON(SDL_BUTTON_LEFT)) {
- // x_vel = -x_vel;
- // y_vel = -y_vel;
- // }
- //update pos
- x_pos += x_vel / 30;
- y_pos += y_vel / 30;
- x_sh += x_sh_vel / 60;
- y_sh += y_sh_vel / 60;
- check(&x_sh_vel, &y_sh_vel, &x_sh, &y_sh, shr.w, shr.h, all_blcks, nmb_blcks);
- if (x_pos <= 0)
- x_pos = 0;
- if (y_pos <= 0)
- y_pos = 0;
- if (x_pos >= WINDOW_WIDTH - all_blcks[0].w)
- x_pos = WINDOW_WIDTH - all_blcks[0].w;
- if (y_pos >= WINDOW_HEIGHT - all_blcks[0].h)
- y_pos = WINDOW_HEIGHT - all_blcks[0].h;
- //set the y pos in the struct
- all_blcks[0].y = (int) y_pos;
- all_blcks[0].x = (int) x_pos;
- all_blcks[1].y = (int) 0;
- all_blcks[1].x = (int) 0;
- all_blcks[2].y = (int) 0;
- all_blcks[2].x = all_blcks[2].w;
- shr.y = (int) y_sh;
- shr.x = (int) x_sh;
- //clear the window
- SDL_RenderClear(rend);
- //draw image to the window
- SDL_RenderCopy(rend, tex_shar, NULL, &shr);
- SDL_RenderCopy(rend, tex, NULL, &all_blcks[0]);
- SDL_RenderCopy(rend, tex1, NULL, &all_blcks[1]);
- SDL_RenderCopy(rend, tex2, NULL, &all_blcks[2]);
- SDL_RenderPresent(rend);
- //update sprite pos
- //y_pos -= (float) SPEED / 60;
- //wait 1/60th of a sec
- SDL_Delay(1000 / 60);
- }
- //clear the window
- SDL_DestroyTexture(tex);
- SDL_DestroyTexture(tex1);
- SDL_DestroyTexture(tex2);
- SDL_DestroyRenderer(rend);
- SDL_DestroyWindow(win);
- SDL_Quit();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement