Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 28.42 KB | None | 0 0
  1. //#include <iostream>
  2. #include <windows.h>
  3. #include <SDL2/SDL.h>
  4. #include <time.h>
  5. #include <math.h>
  6. #include <stdio.h>
  7. #include <stdarg.h>
  8. #include <zlib.h>
  9. #include <vector>
  10. #include <bass.h>
  11. //#include "Mixer.h"
  12. #include "pack.h"
  13.  
  14. #ifdef _DEBUG
  15. #define __BUILD_CONFIG__ "debug"
  16. #else
  17. #define __BUILD_CONFIG__ "release"
  18. #endif
  19.  
  20. #if defined(__amd64__) || defined(_M_X64)
  21. #define __ARCH__ "x64"
  22. #elif defined(__i386__) || defined(_M_IX86) || defined(_X86_)
  23. #define __ARCH__ "x86"
  24. #else
  25. #define __ARCH__ "---"
  26. #endif
  27.  
  28. #define __VERSION__ "rc1"
  29. #define MENU_BACK 0
  30. #define MENU_ACTION 1
  31. /*
  32. #define PACK_SIZE 9144070
  33. #define PACK_CRC32 0x007F4135*/
  34. #define MARKER_CRASH 1097459
  35. //#include "version.h"
  36. /*
  37. TODO:
  38. * elliptic collision
  39. */
  40.  
  41. struct obj {
  42.     int id; // obj texture id
  43.     int x;
  44.     int y; // const = 540?
  45. };
  46.  
  47. std::vector<obj*> objs;
  48.  
  49. using namespace std;
  50.  
  51. //SDL_Surface* spajst, *slupt, *slup2t, *tlot, *turbinat, *pigulat;
  52. SDL_Texture* spajs, *goclaw, *slup, *tlo, *smuteg, *turbina, *fontRender, *pigula, *tlo_cz1, *chmura, *weeia, *cyfry;
  53. SDL_Texture* object[2];
  54. SDL_Texture* slonce1, *slonce2;
  55. SDL_Renderer* render;
  56. SDL_Window* win;
  57. SDL_TimerID timer, uptimer;
  58. SDL_Joystick* joy = NULL;
  59. SDL_Texture* fader; // for fading
  60. bool uiSet = false;
  61. // ---------- AUDIO DECLARE ----------
  62. HSAMPLE spajsW,menuMoveW,menuActionW,menuBackW,menuMusW,punktW,pszegrywW,cheatW;
  63. HCHANNEL musicCh;
  64. HSTREAM stream;
  65. int playMus = 0; // 0 - intro 1 - loop 2 - end
  66. float sndVol = 1.0f;
  67. // ---------- END AUDIO DECLARE ------
  68. bool debug = false;
  69. char font[16 * 94];
  70. int spajscepcja_wearoff;
  71. gzFile pack;
  72. //int przegrano = 0;
  73. int gracz = 0;
  74. SDL_Texture* graczT[2];
  75. char playerName[2][20] = { "SPICE","Goclaw" };
  76. int indeks = 0;
  77. int menuID = 0;
  78. int fade = 0;
  79. int fadeState = 2;
  80. int selPlayer = 0;
  81. char cheatPt = 0;
  82. bool goclawUnlock = false;
  83.  
  84. //void _mixer_main(void* user, Uint8* stream, int len); // TODO: move to CMixer?
  85. void text_center_printf(char* text, int x, int y, int* strBegin, int* strEnd, ...);
  86. //CMixer* mixer;
  87.  
  88. //std::vector<SDL_Rect*> sladSpice;
  89. typedef struct {
  90.     unsigned int offset;
  91.     //unsigned int uncompressedOffset;
  92.     unsigned int size;
  93.     unsigned int orgSize;
  94.     unsigned char name[24];
  95. } fileEntry;
  96. std::vector<fileEntry*> fileDirectory;
  97.  
  98. typedef struct {
  99.     unsigned char player;
  100.     unsigned int score;
  101.     char name[16];
  102. } hallOfFameEntry;
  103.  
  104. hallOfFameEntry hof[10];
  105.  
  106. int state = 0; // 0 - menu, 1 - game, 2 - gameover, 3 - turbina debug, 4 = sun debug
  107. bool turbo = false;
  108. //bool __timerReady = false;
  109.  
  110. struct scianka {
  111.     int dist;
  112.     double passY;
  113.     bool spajscepcja;
  114. };
  115.  
  116. scianka s[100];
  117.  
  118. double sX = 0, sY = 360, sVY, tX, tY;
  119. int frame = 0, uptime = 0, fps = 0, frames = 0;
  120. int ms = 0;
  121. int nextSlup = 0;
  122. double gameSpeed = 1;
  123. Uint8* spajsWav,*spajsCur;
  124. Uint8* przegrywWav;
  125. unsigned int przegrywLen;
  126. unsigned int spajsLen,spajsPos;
  127.  
  128. bool hofInput = false;
  129. int hofCursor = 0;
  130. bool koniec = false;
  131.  
  132. void generateSlupki() {
  133.     if (objs.size() > 0) {
  134.         for (int i = objs.size() - 1; i > 0; i--) {
  135.             delete objs[i];
  136.             objs.pop_back();
  137.         }
  138.     }
  139.     s[0].dist = 700 + (rand() % 500);
  140.     s[0].passY = -400 + (rand() % 300);
  141.  
  142.     for (int i = 1; i<100; i++) {
  143.         s[i].dist = s[i - 1].dist + (500 + (rand() % 500));
  144.         s[i].passY = -200 + (rand() % 300);
  145.         s[i].spajscepcja = (rand() % 100 > 75);
  146.     }
  147.  
  148.     obj* newObj;
  149.     newObj = new obj;
  150.     newObj->x = 540;
  151.     newObj->y = 540-62;
  152.     newObj->id = 0;
  153.     objs.push_back(newObj);
  154. }
  155.  
  156. void renderGUI(int id) {
  157.     SDL_Rect wybor;
  158.     int beg, end;
  159.     switch (id) {
  160.         case 0:
  161.         text_center_printf("Nowa gra", 640, 360, (indeks == 0 ? &beg : NULL), (indeks == 0 ? &end : NULL));
  162.         text_center_printf("Opcje", 640, 360 + 16, (indeks == 1 ? &beg : NULL), (indeks == 1 ? &end : NULL));
  163.         text_center_printf("Wyjscie", 640, 360 + 32, (indeks == 2 ? &beg : NULL), (indeks == 2 ? &end : NULL));
  164.         break;
  165.         case 10:
  166.             char playa[512];
  167.             memset(playa, 0, 512);
  168.             sprintf(playa, "Gracz: %s",playerName[selPlayer]);
  169.             text_center_printf(playa,640,360, (indeks == 0 ? &beg : NULL), (indeks == 0 ? &end : NULL),playa);
  170.             if (joy == NULL) sprintf(playa, "%s", "Nie wykryto pada - podlacz i nacisnij ENTER");
  171.             /*else sprintf(playa, "Ilosc padow: %d, wybrany padad: %s", SDL_NumJoysticks(), SDL_JoystickName(joy));
  172.             text_center_printf(playa,640,360+16,  NULL, NULL);*/
  173.             sprintf(playa, "Dzwiek: %.0f%%", sndVol*100);
  174.             text_center_printf(playa, 640, 360+16, (indeks == 1 ? &beg : NULL), (indeks == 1 ? &end : NULL));
  175.             text_center_printf("Powrot", 640, 360 + 32, (indeks == 2 ? &beg : NULL), (indeks == 2 ? &end : NULL));
  176.             break;
  177.         default:
  178.             text_center_printf("non-existent menu %d !", 640, 360, &beg, &end);
  179.             break;
  180.            
  181.     }
  182.     wybor.x = beg - 16;
  183.     wybor.y = 352 + (indeks * 16);
  184.     wybor.h = 16;
  185.     wybor.w = 16;
  186.     SDL_RenderCopyEx(render, spajs, NULL, &wybor, frame, NULL, SDL_FLIP_NONE);
  187.  
  188.     wybor.x = end;
  189.     SDL_RenderCopyEx(render, spajs, NULL, &wybor, -frame, NULL, SDL_FLIP_NONE);
  190. }
  191.  
  192. void handleGUI(int id,int action) {
  193.     switch (id) {
  194.         case 0: // main menu
  195.             if (action == MENU_BACK) koniec = true;
  196.             else if (action == MENU_ACTION) {
  197.                 switch (indeks) {
  198.                     case 0: // new game
  199.                         state = 1;
  200.                         sX = 0;
  201.                         sY = 360;
  202.                         sVY = 0;
  203.                         turbo = false;
  204.                         nextSlup = 0;
  205.                         ms = 0;
  206.                         spajscepcja_wearoff = 0;
  207.                         generateSlupki();
  208.                         break;
  209.                     case 1: // options
  210.                         menuID = 10;
  211.                         indeks = 0;
  212.                         break;
  213.                     case 2: // exit
  214.                         koniec = true;
  215.                         break;
  216.                 }
  217.             }
  218.             break;
  219.         case 10:
  220.             if (action == MENU_BACK) {
  221.                 indeks = 1;
  222.                 menuID = 0;
  223.             }
  224.             else if (action == MENU_ACTION) {
  225.                 switch (indeks) {
  226.                 case 0: // change player
  227.                     //przegrano++;
  228.                     if(goclawUnlock) selPlayer = (selPlayer + 1) % 2;
  229.                     break;
  230.                 case 1: // change sound volume
  231.                     sndVol += 0.1f;
  232.                     if (sndVol > 1.01f) {
  233.                         sndVol = 0.0f;
  234.                     }
  235.                     BASS_ChannelSetAttribute(musicCh, BASS_ATTRIB_VOL, sndVol*0.7f);
  236.                     break;
  237.                 case 2: // return to main
  238.                     indeks = 0;
  239.                     menuID = 0;
  240.                     break;
  241.                 }
  242.             }
  243.             break;
  244.     }
  245. }
  246.  
  247. void text_printf(char* text, int x, int y, ...) {
  248.     va_list args;
  249.     char buf[1024];
  250.     va_start(args, text);
  251.     memset(buf, 0, 1024);
  252.     vsprintf(buf, text, args);
  253.     va_end(args);
  254.     SDL_SetRenderDrawColor(render, 0, 0, 0, 255);
  255.     for (int k = 0; k<strlen(buf); k++) {
  256.         if (buf[k] >= 0x21 && buf[k] <= 0x7E) {
  257.             int offset = (buf[k] - 0x21) * 16;
  258.             for (int i = 0; i<16; i++) {
  259.                 for (int j = 0; j<8; j++) {
  260.                     if (font[offset + i] & (1 << j)) SDL_RenderDrawPoint(render, (k * 8) + j + x, i + y);
  261.                 }
  262.             }
  263.         }
  264.     }
  265. }
  266.  
  267. void initFade() {
  268.     fader = SDL_CreateTexture(render, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, 1280, 720);
  269.     SDL_SetRenderTarget(render, fader);
  270.     SDL_SetRenderDrawColor(render, 255, 255, 255, 255);
  271.     SDL_RenderClear(render);
  272.     SDL_SetRenderTarget(render, NULL);
  273. }
  274.  
  275. void flashFade() {
  276.     fadeState = 2;
  277.     SDL_Rect r;
  278.     r.x = 0;
  279.     r.y = 0;
  280.     r.w = 1280;
  281.     r.h = 720;
  282.     SDL_SetRenderDrawColor(render, 255, 255, 255, fade);
  283.     if (fadeState == 0) fade++;
  284.     if (fade >= 255 && fadeState == 0) {
  285.         fadeState = 1;
  286.         fade = 255;
  287.     }
  288.     if (fadeState == 1) fade--;
  289.     else if (fadeState == 1 && fade <= 0) {
  290.         fadeState = 2;
  291.         fade = 0;
  292.     }
  293. }
  294.  
  295. void text_center_printf(char* text, int x, int y, int* strBegin, int* strEnd, ...) {
  296.     va_list args;
  297.     int tx, ty;
  298.     char buf[1024];
  299.     va_start(args, text);
  300.     memset(buf, 0, 1024);
  301.     vsprintf(buf, text, args);
  302.     va_end(args);
  303.     SDL_SetRenderDrawColor(render, 0, 0, 0, 255);
  304.     tx = x - ((strlen(buf) * 8) / 2);
  305.     if (strBegin != NULL) *strBegin = tx;
  306.     if (strEnd != NULL) *strEnd = tx + (strlen(buf)*8);
  307.     ty = y - 8;
  308.     for (int k = 0; k<strlen(buf); k++) {
  309.         if (buf[k] >= 0x21 && buf[k] <= 0x7E) {
  310.             int offset = (buf[k] - 0x21) * 16;
  311.             for (int i = 0; i<16; i++) {
  312.                 for (int j = 0; j<8; j++) {
  313.                     if (font[offset + i] & (1 << j)) SDL_RenderDrawPoint(render, (k * 8) + j + tx, i + ty);
  314.                 }
  315.             }
  316.         }
  317.     }
  318. }
  319.  
  320. int getFileByName(char* name, char* data, fileEntry* meta) {
  321.     for (int i = 0; i < fileDirectory.size(); i++) {
  322.         if (!strcmp(name, (char*)fileDirectory[i]->name)) {
  323.             if (meta != NULL) {
  324.                 meta->size = fileDirectory[i]->size;
  325.                 meta->orgSize = fileDirectory[i]->orgSize;
  326.                 meta->offset = fileDirectory[i]->offset;
  327.                 memcpy(meta->name, fileDirectory[i]->name, 24);
  328.                 if (data == NULL) return 1;
  329.             }
  330.             if (data != NULL) {
  331.                 gzseek(pack, fileDirectory[i]->offset, SEEK_SET);
  332.                 gzread(pack, data, fileDirectory[i]->orgSize);
  333.                 return 1;
  334.             }
  335.         }
  336.     }
  337.     return 0;
  338. }
  339.  
  340.  
  341. SDL_Texture* loadBMP(const char* path, bool transparency, bool setIcon = false) {
  342.     SDL_Surface* temp;
  343.     SDL_Texture* texture;
  344.     SDL_RWops* op;
  345.     char* mem;
  346.     fileEntry info;
  347.     char str[512];
  348.     memset(str, 0, 512);
  349.     if(getFileByName((char*)path, NULL, &info)) mem = new char[info.orgSize];
  350.     else {
  351.         sprintf(str, "Nie można wczytać pliku %s (%s)", path, SDL_GetError());
  352.         SDL_ShowSimpleMessageBox(SDL_MessageBoxFlags::SDL_MESSAGEBOX_ERROR, "BÅ‚Ä…d", str, win);
  353.         return NULL;
  354.     }
  355.     getFileByName((char*)path, mem, NULL);
  356.     op = SDL_RWFromMem(mem, info.orgSize);
  357.     temp = SDL_LoadBMP_RW(op, 0);
  358.     delete[] mem;
  359.     if (temp == NULL) {
  360.         sprintf(str, "Nie można wczytać pliku %s (%s)", path, SDL_GetError());
  361.         SDL_ShowSimpleMessageBox(SDL_MessageBoxFlags::SDL_MESSAGEBOX_ERROR, "BÅ‚Ä…d", str, win);
  362.         return NULL;
  363.     }
  364.     if (transparency) SDL_SetColorKey(temp, SDL_TRUE, SDL_MapRGB(temp->format, 255, 128, 255));
  365.     texture = SDL_CreateTextureFromSurface(render, temp);
  366.     if (setIcon) SDL_SetWindowIcon(win, temp);
  367.     SDL_FreeSurface(temp);
  368.     return texture;
  369. }
  370.  
  371. int isFileValid() {
  372.     char* mem;
  373.     fpos_t size;
  374.     FILE* f = fopen("SpiceIsLove.img", "rb");
  375.     if (f == NULL) return 0;
  376.     fseek(f, 0, SEEK_END);
  377.     fgetpos(f, &size);
  378.     fseek(f, 0, SEEK_SET);
  379.     if (size != PACK_SIZE) return 0;
  380.     mem = new char[size];
  381.     fread(mem, 1, size, f);
  382.     fclose(f);
  383.     if (crc32(0, (const Bytef*)mem, size) != PACK_CRC32) {
  384.         delete mem;
  385.         return 0;
  386.     }
  387.     delete mem;
  388.     return 1;
  389. }
  390.  
  391. HSAMPLE loadWAV(char* name, bool loop = false) {
  392.     fileEntry info;
  393.     HSAMPLE temp;
  394.     getFileByName(name, NULL, &info);
  395.     char* mem = new char[info.orgSize];
  396.     getFileByName(name, mem, NULL);
  397.  
  398.     temp = BASS_SampleLoad(TRUE, mem, 0, info.orgSize, 3, (loop == true ? BASS_SAMPLE_LOOP : 0));
  399.  
  400.     delete[] mem;
  401.     mem = NULL;
  402.     return temp;
  403. }
  404.  
  405. void playSound(HSAMPLE spl, bool fadeIn = false, float vol = 1, float pan = 0) {
  406.     HCHANNEL chan = BASS_SampleGetChannel(spl, FALSE);
  407.     BASS_ChannelSetAttribute(chan, BASS_ATTRIB_VOL, vol*sndVol);
  408.     BASS_ChannelSetAttribute(chan, BASS_ATTRIB_PAN, pan);
  409.     if (fadeIn) {
  410.         BASS_ChannelSlideAttribute(chan, BASS_ATTRIB_VOL, 0.7, 5000);
  411.         musicCh = chan;
  412.     }
  413.     BASS_ChannelPlay(chan, TRUE);
  414. }
  415.  
  416. void load() {
  417.     SDL_AudioSpec dummy;
  418.     unsigned int fileCount;
  419.     if (!isFileValid()) {
  420.         SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "BÅ‚Ä…d", "Nie można otworzyć pliku \"SpiceIsLove.img\"", win);
  421.         exit(1);
  422.     }
  423.     pack = gzopen("SpiceIsLove.img", "rb");
  424.     initFade();
  425.     SDL_SetRenderDrawColor(render, 255, 255, 255, 255); // render bg
  426.     SDL_RenderClear(render);
  427.     text_center_printf("Wczytywanie...",640,360,NULL,NULL);
  428.     SDL_RenderPresent(render);
  429.     gzread(pack, &fileCount, 4);
  430.     if (fileCount == 0) {
  431.  
  432.     }
  433.     for (unsigned int i = 0; i < fileCount; i++) {
  434.         fileEntry* entry = new fileEntry;
  435.         gzread(pack, &entry->offset, 4);
  436.         //gzread(pack, &entry->uncompressedOffset, 4);
  437.         gzread(pack, &entry->size, 4);
  438.         gzread(pack, &entry->orgSize, 4);
  439.         //uncompressedSize += entry->orgSize;
  440.         gzread(pack, &entry->name, 24);
  441.         fileDirectory.push_back(entry);
  442.     }
  443.     getFileByName("font.bin", font, NULL);
  444.  
  445.     goclaw = loadBMP("goclaw.bmp", true);
  446.    
  447.     spajs = loadBMP("spajs.bmp",true,true);
  448.     graczT[0] = spajs;
  449.     graczT[1] = goclaw;
  450.     slup = loadBMP("slup.bmp",true);
  451.     //slup2 = loadBMP("slup2.bmp",true);
  452.     tlo = loadBMP("title.bmp",false);
  453.     turbina = loadBMP("turbina.bmp",false);
  454.     pigula = loadBMP("pigula.bmp", false);
  455.     tlo_cz1 = loadBMP("tlo_cz1.bmp", false);
  456.     chmura = loadBMP("chmura.bmp", true);
  457.     weeia = loadBMP("weeia.bmp", true);
  458.     smuteg = loadBMP("smuteg.bmp", true);
  459.     slonce1 = loadBMP("slonce1.bmp", true);
  460.     slonce2 = loadBMP("slonce2.bmp", true);
  461.     cyfry = loadBMP("cyfry.bmp", true);
  462.  
  463.     object[0] = loadBMP("krzak1.bmp", true);
  464.  
  465.     spajsW = loadWAV("spajs.wav");
  466.     menuMusW = loadWAV("loop.wav", true);
  467.     menuActionW = loadWAV("menuAction.wav");
  468.     menuMoveW = loadWAV("menuMove.wav");
  469.     menuBackW = loadWAV("menuBack.wav");
  470.     punktW = loadWAV("punkt.wav");
  471.     pszegrywW = loadWAV("pszegryw2.wav");
  472.     cheatW = loadWAV("cheat.wav");
  473.  
  474.     /*loadWAV("spajs.wav", &spajsWav, &spajsLen); spajsCur = spajsWav; spajsPos = spajsLen; //spajsP = spajsWL;
  475.     loadWAV("przegryw.wav", &przegrywWav, &przegrywLen);
  476.     loadWAV("intro.wav", &introW, &introWL); introP = introWL;
  477.     loadWAV("loop.wav", &loopW, &loopWL); loopP = loopWL;
  478.     musicC = introW;*/
  479.     gzclose(pack);
  480.  
  481.     generateSlupki();
  482.     playSound(menuMusW, true, 0.0f);
  483.  
  484.     FILE* f = fopen("SpiceIsLife.bin", "rb");
  485.     if (f == NULL) {
  486.         f = fopen("SpiceIsLife.bin", "wb");
  487.         for (int i = 0; i < 10; i++) {
  488.             hof[i].player = 0;
  489.             hof[i].score = 0;
  490.             memset(hof[i].name, '.', 15);
  491.             hof[i].name[15] = 0;
  492.         }
  493.         fwrite(hof, 1, sizeof(hallOfFameEntry) * 10, f);
  494.     }
  495.     else {
  496.         fread(hof, 1, sizeof(hallOfFameEntry) * 10, f);
  497.  
  498.     }
  499.     fclose(f);
  500. }
  501.  
  502. int placeInHall() {
  503.     for (int i = 0; i < 10; i++) {
  504.         if (nextSlup >= hof[i].score) return i;
  505.     }
  506.     return -1;
  507. }
  508.  
  509. void jump() {
  510.     if (state != 1) {
  511.         sX = 0;
  512.         sY = 360;
  513.         sVY = 0;
  514.         turbo = false;
  515.         nextSlup = 0;
  516.         ms = 0;
  517.         spajscepcja_wearoff = 0;
  518.         generateSlupki();
  519.         state = 1;
  520.     }
  521.     else {
  522.         if(selPlayer == 0) playSound(spajsW);
  523.         if (turbo) sVY = -1.66*(gameSpeed / 3);
  524.         else sVY = -1.66;
  525.     }
  526. }
  527.  
  528. void free() {
  529.     SDL_FreeWAV(spajsWav);
  530.     SDL_FreeWAV(przegrywWav);
  531.     SDL_DestroyTexture(cyfry);
  532.     SDL_DestroyTexture(object[0]);
  533.     SDL_DestroyTexture(slonce1);
  534.     SDL_DestroyTexture(slonce2);
  535.     SDL_DestroyTexture(fader);
  536.     SDL_DestroyTexture(smuteg);
  537.     SDL_DestroyTexture(goclaw);
  538.     SDL_DestroyTexture(chmura);
  539.     SDL_DestroyTexture(tlo_cz1);
  540.     SDL_DestroyTexture(spajs);
  541.     SDL_DestroyTexture(slup);
  542.     SDL_DestroyTexture(weeia);
  543.     SDL_DestroyTexture(tlo);
  544.     SDL_DestroyTexture(turbina);
  545.     BASS_SampleFree(punktW);
  546.     BASS_SampleFree(spajsW);
  547.     BASS_SampleFree(menuMusW);
  548.     BASS_SampleFree(menuBackW);
  549.     BASS_SampleFree(menuMoveW);
  550.     BASS_SampleFree(menuActionW);
  551.     BASS_SampleFree(pszegrywW);
  552.     BASS_SampleFree(cheatW);
  553.     /*SDL_FreeWAV(spajsW);
  554.     SDL_FreeWAV(loopW);
  555.     SDL_FreeWAV(introW);*/
  556.     //SDL_DestroyTexture(fontRender);
  557. }
  558.  
  559. Uint32 waiter(Uint32 interval, void* param) {
  560.     //__timerReady = true;
  561.     //return 0;
  562.     ms++;
  563.     return interval;
  564. }
  565.  
  566. Uint32 uptimeCounter(Uint32 interval, void* param) {
  567.     uptime++;
  568.     fps = frames;
  569.     frames = 0;
  570.     return interval;
  571. }
  572.  
  573. Uint32 update(Uint32, void*) {
  574.     //if(__timerReady) {
  575.     if (state == 0) {
  576.         if (menuID == 10 && indeks == 0) {
  577.             if ((cheatPt & 7) && (goclawUnlock == false)) {
  578.                 playSound(cheatW);
  579.                 goclawUnlock = true;
  580.             }
  581.         }
  582.     }
  583.     if (state == 1) {
  584.         if (frame > spajscepcja_wearoff) turbo = false;
  585.         else turbo = true;
  586.         if (turbo) gameSpeed = 6;
  587.         else gameSpeed = 1;
  588.         sVY += 0.008*gameSpeed;
  589.         sY += sVY;
  590.         sX+=gameSpeed;
  591.         if (sX > s[nextSlup].dist && sY > (772 + s[nextSlup].passY - 550) && sY < (179 + s[nextSlup].passY + (250))) {
  592.             //SDL_ShowSimpleMessageBox(0,"f","pkt",win);
  593.             if (s[nextSlup].spajscepcja) spajscepcja_wearoff = frame + 300;
  594.             nextSlup++;
  595.             playSound(punktW);
  596.         }
  597.         else if (sX > s[nextSlup].dist && ((sY < (772 + s[nextSlup].passY - 550) || (179 + sY >(s[nextSlup].passY + (250)))))) {
  598.             //__asm int 3;
  599.             //SDL_ShowSimpleMessageBox(0,"uuu","przegryf :/",win);
  600.             //if(joy != NULL) przegrano++;
  601.             spajsCur = przegrywWav;
  602.             spajsPos = przegrywLen;
  603.             if(selPlayer == 0) playSound(pszegrywW);
  604.             if (placeInHall() > -1) {
  605.                 hof[placeInHall()].score = nextSlup;
  606.                 hof[placeInHall()].player = selPlayer;
  607.                 hofInput = true;
  608.                 hofCursor = 0;
  609.                 memset(hof[placeInHall()].name, 0, 16);
  610.             } else nextSlup = 0;
  611.             state = 2;
  612.             sX = 0;
  613.             sY = 360;
  614.             sVY = 0;
  615.             turbo = false;
  616.             ms = 0;
  617.             spajscepcja_wearoff = 0;
  618.             generateSlupki();
  619.         }
  620.     }
  621.     //}
  622.     return 0;
  623. }
  624.  
  625. void blit(SDL_Texture* t, int x, int y, int w, int h) {
  626.     static SDL_Rect r;
  627.     r.x = x;
  628.     r.y = y;
  629.     if (w != 0) r.w = w;
  630.     if (h != 0) r.h = h;
  631.     SDL_RenderCopy(render, t, NULL, &r);
  632. }
  633.  
  634. void draw() {
  635.     SDL_Rect spajsr, slupr[200], turbinar, rect, src, dst; // TODO: this can be done with just one "slupr"
  636.     spajsr.w = 143;
  637.     spajsr.h = 179;
  638.     spajsr.x = 60;
  639.     spajsr.y = sY;
  640.     rect.w = 32;
  641.     rect.h = 32;
  642.     //if(__timerReady) {
  643.  
  644.     if(fadeState == 2) SDL_SetRenderDrawColor(render, 123, 166, 237, 255); // render bg
  645.     else  SDL_SetRenderDrawColor(render, 0,0,0, 255); // render bg
  646.  
  647.     SDL_RenderClear(render);
  648.     if (state == 0) {
  649.             SDL_RenderCopy(render, tlo, NULL, NULL);
  650.             text_printf("version " __VERSION__, 0, 688);
  651.             text_printf("build date: " __DATE__ " " __TIME__ " (" __ARCH__ " " __BUILD_CONFIG__ " build)", 0, 704);
  652.             renderGUI(menuID);
  653.     }
  654.     else if (state == 1) {
  655.  
  656.         if (turbo) {
  657.             tX = cos((double)frame / 50) * 100;//(30-sin((double)frame/100)*20))*100;
  658.             tY = sin((double)frame / 50) * 100;//(30+cos((double)frame/100)*20))*100;
  659.             for (int i = -1; i<15; i++) {
  660.                 for (int j = -1; j<15; j++) {
  661.                     turbinar.x = j * 377 + tX;
  662.                     turbinar.y = i * 496 + tY;
  663.                     turbinar.w = 377;
  664.                     turbinar.h = 496;
  665.                     SDL_RenderCopy(render, turbina, NULL, &turbinar);
  666.                     SDL_RenderCopyEx(render, turbina, NULL, &turbinar, frame, NULL, SDL_FLIP_NONE);
  667.                 }
  668.             }
  669.         }
  670.  
  671.         rect.x = 0;
  672.         rect.y = 0;
  673.         rect.w = 300;
  674.         rect.h = 300;
  675.         SDL_RenderCopyEx(render, slonce1, NULL, &rect, frame, NULL, SDL_FLIP_NONE);
  676.         SDL_RenderCopy(render, slonce2, NULL, &rect);
  677.  
  678.         for (int i = 0; i < (1280 / 24) + 2; i++) {
  679.             rect.y = 720 - 180;
  680.             rect.x = -24 + (((int)sX%24) + (i*24));
  681.             rect.w = 24;
  682.             rect.h = 180;
  683.             SDL_RenderCopy(render, tlo_cz1, NULL, &rect);
  684.         }
  685.  
  686.         for (int i = 0; i < objs.size(); i++) {
  687.             rect.x = objs[i]->x - (sX/70);
  688.             rect.y = objs[i]->y;
  689.             SDL_QueryTexture(object[objs[i]->id], NULL, NULL, &rect.w, &rect.h);
  690.             SDL_RenderCopy(render, object[objs[i]->id], NULL, &rect);
  691.         }
  692.  
  693.         rect.x = 1000 - (sX / 70);
  694.         rect.y = 180;
  695.         rect.w = 353;
  696.         rect.h = 204;
  697.         SDL_RenderCopy(render, chmura, NULL, &rect);
  698.  
  699.         rect.x = 512 - (sX / 50);
  700.         rect.y = 360;
  701.         rect.w = 720;
  702.         rect.h = 180;
  703.         SDL_RenderCopy(render, weeia, NULL, &rect);
  704.  
  705.         //SDL_Texture* plyr = spajs;//((przegrano % 2) && joy != NULL ? goclaw : spajs);
  706.  
  707.         if (!turbo)  SDL_RenderCopyEx(render, graczT[selPlayer], NULL, &spajsr, sVY * 18, NULL, SDL_FLIP_NONE);
  708.         else SDL_RenderCopyEx(render, graczT[selPlayer], NULL, &spajsr, (-frame * 2) + sVY * 18, NULL, SDL_FLIP_NONE);
  709.         for (int i = 0; i<100; i++) {
  710.             slupr[(i * 2)].x = s[i].dist - sX;
  711.             slupr[(i * 2)].y = s[i].passY - 566;
  712.             slupr[(i * 2)].w = 133;
  713.             slupr[(i * 2)].h = 772;
  714.  
  715.             SDL_RenderCopyEx(render, slup, NULL, &slupr[(i * 2)], 0, NULL, SDL_FLIP_VERTICAL);
  716.  
  717.  
  718.             if (s[i].spajscepcja) {
  719.                 rect.x = s[i].dist + 16 - sX;
  720.                 rect.y = s[i].passY + 250;
  721.                 rect.w = 32;
  722.                 rect.h = 32;
  723.                 //SDL_SetRenderDrawColor(render, 255, 0, 0, 255);
  724.                 //SDL_RenderFillRect(render, &rect);
  725.                 SDL_RenderCopy(render, pigula, NULL, &rect);
  726.             }
  727.  
  728.             slupr[(i * 2) + 1].x = s[i].dist - sX;
  729.             slupr[(i * 2) + 1].y = s[i].passY + 666;
  730.             slupr[(i * 2) + 1].w = 133;
  731.             slupr[(i * 2) + 1].h = 772;
  732.  
  733.             if(s[i].dist - sX > -133 && s[i].dist - sX < 1413) SDL_RenderCopy(render, slup, NULL, &slupr[(i * 2) + 1]);
  734.             /*if (debug) text_printf("FPS: %d, uptime: %d:%.2d, frame: %d, ms: %d, Y: %f, vY: %f, nextSlup: %d, passY: %f, minY: %f, maxY: %f", 0, 0, fps, uptime / 60, uptime % 60, frame, ms, sY, sVY, nextSlup, s[nextSlup].passY, double(s[nextSlup].passY - 550), double(s[nextSlup].passY + 550)); {
  735.                
  736.  
  737.             }
  738.             //else text_printf("%d",0,0,nextSlup);*/
  739.             /* -- UI START -- */
  740.                 src.y = 0;
  741.                 src.w = 32;
  742.                 src.h = 48;
  743.  
  744.                
  745.                 dst.y = 96;
  746.                 dst.w = 32;
  747.                 dst.h = 48;
  748.             src.x = (nextSlup / 10) * 32;
  749.             dst.x = 624;
  750.             SDL_RenderCopy(render, cyfry, &src, &dst);
  751.  
  752.             src.x = (nextSlup % 10) * 32;
  753.             dst.x = 656;
  754.             SDL_RenderCopy(render, cyfry, &src, &dst);
  755.         }
  756.     }
  757.     else if (state == 2) {
  758.         double ccX, ccY;
  759.         int beg;
  760.         char hofText[512];
  761.         // table start 208
  762.         text_center_printf("Najlepsze wyniki: ", 640, 176, NULL, NULL);
  763.  
  764.         for (int i = 0; i < 10; i++) {
  765.             memset(hofText, 0, 512);
  766.             sprintf(hofText, "%d %s", hof[i].score, hof[i].name);
  767.             text_center_printf(hofText, 640, 208 + (i*16), &beg, NULL);
  768.             blit(graczT[hof[i].player], beg - 16, 208 + (i * 16) - 8, 16, 16);
  769.         }
  770.  
  771.         memset(hofText, 0, 512);
  772.         if(placeInHall() > - 1) sprintf(hofText, "Zajales %d. miejsce, brawo, wpisz sie i nacisnij ENTER aby zapisac wyniki, ESC aby wyjsc do menu.",placeInHall()+1);
  773.         else sprintf(hofText, "Niestety, nie udalo ci sie zdobyc zadnego miejsca. Wcisnij ENTER lub ESC aby powrocic do menu.");
  774.         text_center_printf(hofText, 640, 400,NULL,NULL);
  775.         ccX = 640 - (267 / 2) + double(cos(double(frame/66)) * 400);
  776.         ccY = 400 + double(sin(double(frame/40))* 100);
  777.         rect.x = ccX;
  778.         rect.y = ccY;
  779.         rect.w = 267;
  780.         rect.h = 324;
  781.         SDL_RenderCopy(render, smuteg, NULL, &rect);
  782.     }
  783.     /*else if (state == 3) {
  784.         // moved to state 1 (game)
  785.     }
  786.     else if (state == 4) {
  787.         SDL_Rect r;
  788.         r.x = 0;
  789.         r.y = 0;
  790.         r.w = 300;
  791.         r.h = 300;
  792.         SDL_RenderCopyEx(render, slonce1, NULL, &r, frame, NULL, SDL_FLIP_NONE);
  793.         SDL_RenderCopy(render, slonce2, NULL, &r);
  794.     }*/
  795.     else {
  796.         text_center_printf("error unknown state %d", 640, 360, NULL, NULL, state);
  797.     }
  798.     //if(debug) printf("build %d (%s/%s/%s)",0,8,AutoVersion::BUILD,AutoVersion::DATE,AutoVersion::MONTH,AutoVersion::YEAR);
  799.     //SDL_RenderCopy(render,fontRender,NULL,NULL);
  800.     SDL_RenderPresent(render);
  801.     frame = SDL_GetTicks() / 10;
  802.     frames++;
  803.     //__timerReady = false;
  804.     //}
  805.     //SDL_Delay(2);
  806. }
  807.  
  808. int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
  809. {
  810.     SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK);
  811.     atexit(SDL_Quit);
  812.     win = SDL_CreateWindow("Flappy Spice " __VERSION__ " (" __DATE__ " " __TIME__ " - " __ARCH__ " " __BUILD_CONFIG__ " build)", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, 0);
  813.     render = SDL_CreateRenderer(win, -1, 0);
  814.     SDL_ShowWindow(win);
  815.     SDL_Event ev;
  816.     srand(time(NULL));
  817.     BASS_Init(-1, 44100, BASS_DEVICE_STEREO, 0, NULL);
  818.     load();
  819.     if (SDL_NumJoysticks() > 0) {
  820.         joy = SDL_JoystickOpen(0);
  821.     }
  822.     else {
  823.         joy = NULL;
  824.     }
  825.     /*SDL_AudioSpec format;
  826.     memset(&format, 0, sizeof(SDL_AudioSpec));
  827.     format.freq = 44100;
  828.     format.channels = 2;
  829.     format.format = AUDIO_S16;
  830.     format.samples = 4096;
  831.     format.callback = _mixer_main;
  832.     audio = SDL_OpenAudioDevice(NULL, 0, &format, NULL, 0);
  833.  
  834.     spajsCur = spajsWav;
  835.     spajsPos = spajsLen;*/
  836.  
  837.     //mixer = new CMixer();
  838.  
  839.     //SDL_PauseAudioDevice(audio, 0);
  840.     //mixer->play(MIXER_STREAM, (unsigned char*) "getabrain_loop.wav", 0, 1);
  841.  
  842.  
  843.     timer = SDL_AddTimer(1, waiter, NULL);
  844.     uptimer = SDL_AddTimer(10, uptimeCounter, NULL);
  845.     while (!koniec) {
  846.         while (SDL_PollEvent(&ev)) {
  847.             switch (ev.type) {
  848.             case SDL_QUIT:
  849.                 koniec = true;
  850.                 break;
  851.             case SDL_KEYUP:
  852.                 switch (ev.key.keysym.sym) {
  853.                 case SDLK_7:
  854.                 case SDLK_5:
  855.                 case SDLK_4:
  856.                     cheatPt = 0;
  857.                     break;
  858.                 }
  859.                 break;
  860.             case SDL_KEYDOWN:
  861.                 if (state == 0) {
  862.                     switch (ev.key.keysym.sym) {
  863.                         case SDLK_UP:
  864.                             indeks = ((indeks == 0 ? indeks = 2 : --indeks)) % 3;
  865.                             playSound(menuMoveW);
  866.                             break;
  867.                         case SDLK_DOWN:
  868.                             indeks = (++indeks) % 3;
  869.                             playSound(menuMoveW);
  870.                             break;
  871.                         case SDLK_RETURN:
  872.                             handleGUI(menuID, MENU_ACTION);
  873.                             playSound(menuActionW);
  874.                             break;
  875.                         case SDLK_ESCAPE:
  876.                             handleGUI(menuID, MENU_BACK);
  877.                             playSound(menuBackW);
  878.                         case SDLK_7:
  879.                             cheatPt = cheatPt | 1;
  880.                             break;
  881.                         case SDLK_5:
  882.                             cheatPt = cheatPt | 2;
  883.                             break;
  884.                         case SDLK_4:
  885.                             cheatPt = cheatPt | 4;
  886.                             break;
  887.                     }
  888.                 }
  889.                 else if (state == 1) {
  890.                     //if (przegrano % 2 == 0) {
  891.                         if (state != 1) state = 1;
  892.                         if (state == 1 && ev.key.keysym.sym == SDLK_t) turbo = !turbo;
  893.                         if (state == 1 && ev.key.keysym.sym == SDLK_d) debug = !debug;
  894.                         if (state == 1 && ev.key.keysym.sym == SDLK_SPACE) jump();
  895.                     //}
  896.                 }
  897.                 else if (state == 2) {
  898.                     if (hofInput) {
  899.                         int place = placeInHall();
  900.                         if (ev.key.keysym.sym == SDLK_BACKSPACE) {
  901.                             hof[place].name[hofCursor] = 0;
  902.                             if (hofCursor > 0) hofCursor--;
  903.                         }
  904.                         else if (ev.key.keysym.sym == SDLK_RETURN) {
  905.                             hofInput = false;
  906.                             FILE* f = fopen("SpiceIsLife.bin", "wb");
  907.                             fwrite(hof, 1, sizeof(hallOfFameEntry) * 10, f);
  908.                             fclose(f);
  909.                             playSound(menuBackW);
  910.                             sX = 0;
  911.                             sY = 360;
  912.                             sVY = 0;
  913.                             turbo = false;
  914.                             ms = 0;
  915.                             spajscepcja_wearoff = 0;
  916.                             generateSlupki();
  917.                             state = 0;
  918.                             indeks = 0;
  919.                             menuID = 0;
  920.                         }
  921.                         else {
  922.                             if (hofCursor < 15) {
  923.                                 hof[place].name[hofCursor] = ev.key.keysym.sym;
  924.                                 hofCursor++;
  925.                             }
  926.                         }
  927.                     }
  928.                 }
  929.                 if (ev.key.keysym.sym == SDLK_ESCAPE && state != 0) {
  930.                     playSound(menuBackW);
  931.                     state = 0;
  932.                     sX = 0;
  933.                     sY = 360;
  934.                     nextSlup = 0;
  935.                     sVY = 0;
  936.                     turbo = false;
  937.                     ms = 0;
  938.                     spajscepcja_wearoff = 0;
  939.                     generateSlupki();
  940.                 }
  941.                 if (ev.key.keysym.sym == SDLK_F12) {
  942.                     SDL_Surface *sshot = SDL_CreateRGBSurface(0, 1280, 720, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
  943.                     SDL_RenderReadPixels(render, NULL, SDL_PIXELFORMAT_ARGB8888, sshot->pixels, sshot->pitch);
  944.                     SDL_SaveBMP(sshot, "screen.bmp");
  945.                     SDL_FreeSurface(sshot);
  946.                 }
  947.                 break;
  948.             case SDL_JOYAXISMOTION:
  949.                 if (state == 0) {
  950.                     if (ev.jaxis.value < -16384 && ev.jaxis.axis == 1) {
  951.                         indeks = ((indeks == 0 ? indeks = 2 : --indeks)) % 3;
  952.                         playSound(menuMoveW);
  953.                     }
  954.                     else if (ev.jaxis.value > 16384 && ev.jaxis.axis == 1) {
  955.                         indeks = (++indeks) % 3;
  956.                         playSound(menuMoveW);
  957.                     }
  958.                 }
  959.                 else if (state == 1) {
  960.                     if (ev.jaxis.value < -16384 && ev.jaxis.axis == 1) {
  961.                         jump();
  962.                     }
  963.                 }
  964.                 break;
  965.             case SDL_JOYBUTTONDOWN:
  966.                 if (state == 0) {
  967.                     if (ev.jbutton.button == 1) { // circle button
  968.                         handleGUI(menuID,MENU_BACK);
  969.                         playSound(menuBackW);
  970.                     } else if (ev.jbutton.button == 2) { // "X" button
  971.                         handleGUI(menuID, MENU_ACTION);
  972.                         playSound(menuActionW);
  973.                     }
  974.                     if (ev.jbutton.button == 1 && state != 0) {
  975.                         playSound(menuBackW);
  976.                         state = 0;
  977.                         sX = 0;
  978.                         sY = 360;
  979.                         nextSlup = 0;
  980.                         sVY = 0;
  981.                         turbo = false;
  982.                         ms = 0;
  983.                         spajscepcja_wearoff = 0;
  984.                         generateSlupki();
  985.                     }
  986.                 } else if(state == 1) jump();
  987.                 break;
  988.             case SDL_JOYDEVICEADDED:
  989.                 if (SDL_NumJoysticks() > 0) joy = SDL_JoystickOpen(0);
  990.                 break;
  991.             case SDL_JOYDEVICEREMOVED:
  992.                 if (SDL_NumJoysticks() <= 0) joy = NULL;
  993.                 break;
  994.             case SDL_MOUSEBUTTONDOWN:
  995.                 if(state == 1) jump();
  996.                 break;
  997.             }
  998.         }
  999.         if (ms > 0) {
  1000.             update(1, NULL);
  1001.             draw();
  1002.             ms = 0;
  1003.         }
  1004.     }
  1005.     free();
  1006.     //SDL_CloseAudioDevice(audio);
  1007.     //delete mixer;
  1008.     SDL_JoystickClose(joy);
  1009.     BASS_Free();
  1010.     SDL_RemoveTimer(timer);
  1011.     SDL_RemoveTimer(uptimer);
  1012.     SDL_DestroyRenderer(render);
  1013.     SDL_DestroyWindow(win);
  1014.     return 0;
  1015. }
  1016.  
  1017. // --------------------- AUDIO MIXER --------------------------
  1018.  
  1019. void mixSpiceStream(Uint8* stream, int len) {
  1020.     for (int i = 0; i < len; i++) {
  1021.  
  1022.     }
  1023. }
  1024.  
  1025. void _mixer_main(void* user, Uint8* stream, int len) {
  1026.     memset(stream, 128, len); // silence
  1027.     /*
  1028.     switch (playMus) {
  1029.         case 0:
  1030.             if (introP > 0) {
  1031.                 memcpy(stream, musicC, len);
  1032.                 if (introP > len) {
  1033.                     musicC += len;
  1034.                     introP -= len;
  1035.                 }
  1036.                 else {
  1037.                     musicC += introP;
  1038.                     introP = 0;
  1039.                 }
  1040.             }
  1041.             else {
  1042.                 playMus++;
  1043.                 musicC = loopW;
  1044.                 loopP = loopWL;
  1045.             }
  1046.             break;
  1047.         case 1:
  1048.             if (loopP > 0) {
  1049.                 memcpy(stream, musicC, len);
  1050.                 if (loopP > len) {
  1051.                     musicC += len;
  1052.                     loopP -= len;
  1053.                 }
  1054.                 else {
  1055.                     musicC += introP;
  1056.                     loopP = 0;
  1057.                 }
  1058.             }
  1059.             else {
  1060.                 musicC = loopW;
  1061.                 loopP = loopWL;
  1062.             }
  1063.             break;
  1064.     }
  1065.     mixSpiceStream();*/
  1066.     if (spajsPos > 0) {
  1067.         //SDL_MixAudio(stream, spajsCur, len, SDL_MIX_MAXVOLUME);
  1068.         memcpy(stream, spajsCur, len);
  1069.         if (spajsPos > len) {
  1070.             spajsCur += len;
  1071.             spajsPos -= len;
  1072.         }
  1073.         else {
  1074.             spajsCur += spajsPos;
  1075.             spajsPos -= spajsPos;
  1076.             return;
  1077.         }
  1078.     }
  1079.     //mixer->playAudio(stream, len);
  1080. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement