Advertisement
Coding_guy_

Src

Aug 6th, 2023
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.00 KB | Source Code | 0 0
  1. //gfx_BlitRectangle
  2.  
  3. #include <stdio.h>
  4. #include <fileioc.h>
  5. #include <keypadc.h>
  6. #include <graphx.h>
  7. #include "gfx/gfx.h"
  8. #include <sys/timers.h>
  9. #include <math.h>
  10.  
  11. typedef struct {
  12.     double x;
  13.     double y;
  14. } Point;
  15.  
  16. struct Tilemap {
  17.     gfx_sprite_t **sprites; // Array of array of pointers to sprite data for each frame
  18.     int sprites_num;
  19.     int **sprite_matrix;
  20. };
  21.  
  22. struct SpriteInfo {
  23.     gfx_sprite_t **frames; // Array of pointers to sprite data for each frame
  24.     int num_frames;
  25.     int current_frame;
  26. };
  27.  
  28. int findNearestTile(struct Tilemap *tilemap, int tileX, int tileY) {
  29.     // Return the pointer to the nearest tile sprite
  30.     return tilemap->sprite_matrix[tileY / 16][tileX / 16]; // / by 16 because it needs screen pixel to matrix
  31. }
  32.  
  33. double getDistance(Point point1, Point point2){
  34.     double dx = point1.x - point2.x;
  35.     double dy = point1.y - point2.y;
  36.     double distance = sqrt(dx * dx + dy * dy);
  37.     return distance;
  38. }
  39.  
  40. Point closestJump(struct Tilemap *tilemap, int playerX, int playerY){
  41.     double closest = 10000;
  42.     Point closestPoint = {0, 0};
  43.     double dist = 0;
  44.     for (int i = 0; i < 20; i++) {
  45.         for (int j = 0; j < 20; j++) {
  46.             if(tilemap->sprite_matrix[i][j] == 1){
  47.                 dist = getDistance((Point) {i * 16, j * 16}, (Point) {playerX, playerY});
  48.                 if(dist < closest){
  49.                     closest = dist;
  50.                     closestPoint = (Point) {i * 16, j * 16};
  51.                 }
  52.             }
  53.         }
  54.     }
  55.     return closestPoint;
  56. }
  57.  
  58. void sprite(struct SpriteInfo *spriteInfo, int posX, int posY) {//second used to be Tilemap IMPORTANT if you want to bring the tile drawing back
  59.     // int tileX = (round(posX / 16)) * 16;
  60.     // int tileY = (round(posY / 16)) * 16;
  61.     // if(tileY == 240){
  62.     //     tileY = 224;
  63.     // }
  64.     // int Tile = findNearestTile(tilemap, tileX, tileY);
  65.     //gfx_Sprite(tilemap->sprites[Tile], tileX, tileY);
  66.  
  67.     gfx_TransparentSprite(spriteInfo->frames[spriteInfo->current_frame], posX, posY);
  68. }
  69.  
  70. void animatedSprite(struct SpriteInfo *spriteInfo, int posX, int posY) { //second used to be Tilemap IMPORTANT if you want to bring the tile drawing back
  71.     // int tileX = (round(posX / 16)) * 16;
  72.     // int tileY = (round(posY / 16)) * 16;
  73.     // if(tileY == 240){
  74.     //     tileY = 224;
  75.     // }
  76.     // int Tile = findNearestTile(tilemap, tileX, tileY);
  77.     //gfx_Sprite(tilemap->sprites[Tile], tileX, tileY);
  78.  
  79.     gfx_TransparentSprite(spriteInfo->frames[spriteInfo->current_frame], posX, posY);
  80.     if (++spriteInfo->current_frame >= spriteInfo->num_frames) {
  81.         spriteInfo->current_frame = 0;
  82.     }
  83. }
  84.  
  85. void drawTileset(struct Tilemap tileset){
  86.     for (int i = 0; i < 20; i++) {
  87.         for (int j = 0; j < 20; j++) {
  88.             gfx_Sprite(tileset.sprites[tileset.sprite_matrix[i][j]], i * 16, j * 16);
  89.         }
  90.     }
  91. }
  92.  
  93. Point calculateTrajectory(double x0, double y0, double x1, double y1, double time_ms, int originalX0, int originalY0) {
  94.     double t_travel = (fabs(originalX0 - x1) / 70.0) + .1; //seconds
  95.     double v0 = (x1 - x0) / t_travel;
  96.     double a = 2 * (y1 - y0 - v0 * t_travel) / (t_travel * t_travel);
  97.     double vx = v0;
  98.  
  99.     double t = time_ms / 1000.0; // Convert time_passed to seconds
  100.  
  101.     // Calculate the y-coordinate of the control point
  102.     double control_point_y = y0 + 1 * (y1 - y0);
  103.  
  104.     Point point;
  105.     point.x = x0 + vx * t;
  106.     point.y = y0 + v0 * t + 0.5 * a * t * t;
  107.  
  108.     // Adjust the y-coordinate based on the height_factor
  109.     point.y += (control_point_y - point.y) * (2 * t / t_travel) * (1 - t / t_travel);
  110.    
  111.     if(point.y >= y1 - 1 && point.y <= y1 + 1){
  112.         point.y = y1;
  113.     }
  114.     if(point.x >= x1 - 1 && point.x <= x1 + 1){
  115.         point.x = x1;
  116.     }
  117.     return point;
  118. }
  119.  
  120. int main() {
  121.     gfx_Begin();
  122.     gfx_FillScreen(1);
  123.     gfx_SetDraw(gfx_buffer);
  124.  
  125.     struct Tilemap tileMap;
  126.     tileMap.sprites_num = 2;
  127.     tileMap.sprites = malloc(sizeof(gfx_sprite_t *) * tileMap.sprites_num);
  128.     tileMap.sprites[0] = untitled;
  129.     tileMap.sprites[1] = JumpingTile;
  130.  
  131.     // Dynamically allocate memory for the sprite matrix
  132.     int matrix_size = 20;
  133.     tileMap.sprite_matrix = malloc(sizeof(int *) * matrix_size);
  134.     for (int i = 0; i < matrix_size; i++) {
  135.         tileMap.sprite_matrix[i] = malloc(sizeof(int) * matrix_size);
  136.         for (int j = 0; j < matrix_size; j++) {
  137.             tileMap.sprite_matrix[i][j] = 0;
  138.             gfx_SetDraw(gfx_buffer);
  139.             gfx_Sprite(untitled, i * 16, j * 16);
  140.             gfx_SetDraw(gfx_screen);
  141.             gfx_Sprite(untitled, i * 16, j * 16);
  142.         }
  143.     }
  144.     tileMap.sprite_matrix[1][14] = 1;
  145.     tileMap.sprite_matrix[7][14] = 1;
  146.             gfx_SetDraw(gfx_buffer);
  147.             gfx_Sprite(JumpingTile, 1 * 16, 19 * 16);
  148.             gfx_SetDraw(gfx_screen);
  149.             gfx_Sprite(JumpingTile, 1 * 16, 19 * 16);
  150.    
  151.  
  152.  
  153.     struct SpriteInfo cat;
  154.     cat.num_frames = 4;
  155.     cat.current_frame = 0;
  156.     cat.frames = malloc(sizeof(gfx_sprite_t *) * cat.num_frames * 16);
  157.     cat.frames[0] = cat1;
  158.     cat.frames[1] = cat2;
  159.     cat.frames[2] = cat3;
  160.     cat.frames[3] = cat4;
  161.     gfx_SetPalette(global_palette, sizeof_global_palette, 0);
  162.  
  163.     /* These were set in the image conversion file */
  164.     gfx_SetTransparentColor(0);
  165.     int animationMS = 33;//83
  166.     int msSinceLast = 0;
  167.     int msStart = 0; // animation reset
  168.     int msLoopStart = 0; //loop reset
  169.     float playerX = 100;
  170.     int playerY = 240 - 15; // - sprite size
  171.     float playerSpeed = .1;
  172.     bool isJumping = false;
  173.     int jumpmsPassed = 0;
  174.     bool firstJump = true;
  175.     int originalX = 0;
  176.     int originalY = 0;
  177.     int playerOffsetY = 15;
  178.     Point nearestJump = {0, 0};
  179.     gfx_SetTextXY(100, 100);
  180.     gfx_SetDraw(gfx_buffer);
  181.     timer_Set(1, 0);
  182.     timer_Enable(1, TIMER_32K, TIMER_NOINT, TIMER_UP);
  183.  
  184.     while (!(kb_Data[6] & kb_Clear)) {
  185.         msLoopStart = timer_Get(1)/32.768;
  186.         kb_Scan();
  187.         if(!isJumping){
  188.             if(kb_Data[7] & kb_Left){
  189.                 playerX -= playerSpeed * msSinceLast;
  190.             }
  191.             if(kb_Data[7] & kb_Right){
  192.                 playerX += playerSpeed * msSinceLast;
  193.             }
  194.             if(kb_Data[1] & kb_2nd){
  195.                 isJumping = true;
  196.             }
  197.         }else{
  198.             jumpmsPassed += msSinceLast;
  199.             if(firstJump){
  200.                 originalX = playerX;
  201.                 originalY = playerY;
  202.                 firstJump = false;
  203.                 nearestJump.y -= playerOffsetY;
  204.             }
  205.            
  206.             Point traj = calculateTrajectory(playerX, playerY, nearestJump.x, nearestJump.y, jumpmsPassed, originalX, originalY); // subtract 17 because of the position
  207.             playerX = traj.x;
  208.             playerY = traj.y;
  209.             if(playerX == nearestJump.x && playerY == nearestJump.y){
  210.                 firstJump = true;
  211.                 isJumping = false;
  212.                 jumpmsPassed = 0;
  213.             }
  214.         }
  215.  
  216.         if(msLoopStart - msStart >= animationMS){
  217.             //gfx_FillRectangle(playerX, playerY, 16, 16);
  218.             animatedSprite(&cat, (int) playerX, playerY); //second was , &tileMap
  219.             gfx_SwapDraw();
  220.             drawTileset(tileMap);
  221.             if(!isJumping){
  222.                 nearestJump = closestJump(&tileMap, playerX, playerY);
  223.                 gfx_SetColor(0);
  224.                 gfx_Rectangle(nearestJump.x, nearestJump.y, 2, 2);
  225.             }
  226.             msStart = msLoopStart;
  227.             //timer_Set(1, 0);
  228.         }else{
  229.             sprite(&cat, (int) playerX, playerY); // , &tileMap
  230.             gfx_SwapDraw();
  231.         }
  232.  
  233.         msSinceLast = timer_Get(1)/32.768 - msLoopStart;
  234.     }
  235.     timer_Disable(1);
  236.     // Free the dynamically allocated memory before exiting the program
  237.     for (int i = 0; i < matrix_size; i++) {
  238.         free(tileMap.sprite_matrix[i]);
  239.     }
  240.     free(tileMap.sprite_matrix);
  241.     free(tileMap.sprites);
  242.  
  243.     free(cat.frames);
  244.  
  245.     gfx_End();
  246.     return 0;
  247. }
  248.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement