2D_Game_Engine
Loading...
Searching...
No Matches
scene_buttons.cpp
Go to the documentation of this file.
1#include <pch.hpp>
2#include <scene_buttons.hpp>
3#include <utilities.hpp>
4#include <editor.hpp>
5
9void GenerateApplication(std::vector<std::pair<std::string,uint32_t>>&script_components,
10 std::string &window_name,unsigned int width,unsigned int height,unsigned int fullscreen_width,
11 unsigned int fullscreen_height,std::string &scene_path,TonemapType tonemap,float gamma,float exposure,bool resizable=false){
12 FILE *fdest=fopen("temp/data.cpp","w");
13 if(fdest==NULL){
14 perror("Failed to open file temp/data.cpp");
15 return;
16 }
17
18 fprintf(fdest,"#include <pch.hpp>\n");
19 fprintf(fdest,"#include <scene.hpp>\n\n");
20 fprintf(fdest,"const char *WINDOW_NAME=\"%s\";\n",window_name.c_str());
21 fprintf(fdest,"unsigned int WINDOW_WIDTH=%u;\n",width);
22 fprintf(fdest,"unsigned int WINDOW_HEIGHT=%u;\n",height);
23 fprintf(fdest,"unsigned int FULLSCREEN_WIDTH=%u;\n",fullscreen_width);
24 fprintf(fdest,"unsigned int FULLSCREEN_HEIGHT=%u;\n",fullscreen_height);
25 fprintf(fdest,"std::string SCENE_PATH=\"%s\";\n",scene_path.c_str());
26 fprintf(fdest,"bool RESIZABLE=%s;\n",((resizable)?"true":"false"));
27 fprintf(fdest,"TonemapType TONE_MAP_TYPE=TonemapType::%s;\n",TonemapTypeToString(tonemap).c_str());
28 fprintf(fdest,"float GAMMA=%f;\n",gamma);
29 fprintf(fdest,"float EXPOSURE=%f;\n",exposure);
30
31 for(auto &[fn_name,id]:script_components){ //generate the function prototypes for the native scripts
32 if(!fn_name.empty()){
33 fprintf(fdest,"extern void %s(Scene *scene,NativeScriptComponent *nsc,float frame_time);\n",fn_name.c_str());
34 }
35 }
36
37 fprintf(fdest,"\nvoid LoadScripts(Scene *scene){\n");
38
39 int i=0;
40 for(auto &[fn_name,id]:script_components){ //bind the function pointers to the native scripts
41 if(!fn_name.empty()){
42 fprintf(fdest,"\tscene->AddComponent<NativeScriptComponent>((uint32_t)%u);\n",id);
43 fprintf(fdest,"\tauto nsc%d=scene->GetComponent<NativeScriptComponent>((uint32_t)%u)->OnUpdate=%s;\n",i,id,fn_name.c_str());
44 i++;
45 }
46 }
47
48 fprintf(fdest,"}");
49
50 fclose(fdest);
51}
52
53void SceneButtons::PlayButton(std::vector<std::pair<std::string,uint32_t>>&script_components){
54 static std::thread applicationThread;
55 static std::atomic<bool> isApplicationRunning=false;
56
57 if(isApplicationRunning.load()){ //application is already running, return
58 return;
59 }
60
61 isApplicationRunning.store(true);
62 applicationThread=std::thread([&isApplicationRunning,&script_components](){ //run the application in another thread
63 std::string scene_path="test.scene";
64 ExecuteCommand("mkdir -p temp");
67 printf("%s\n",ExecuteCommand("cd application && premake5 gmake2 --file=application_premake.lua").c_str());
68 #if defined(RELEASE)
69 printf("%s\n",ExecuteCommand("cd lib && make config=release").c_str());
70 printf("%s\n",ExecuteCommand("cd temp && make config=release").c_str());
71 ExecuteCommand("mkdir -p bin/Release/resources");
72 ExecuteCommand("cp -r resources/* bin/Release/resources");
73 char command[256];
74 command[0]='\0';
75 strcat(command,"cp ");
76 strcat(command,SCENE_PATH.c_str());
77 strcat(command," bin/Release/");
78 strcat(command,SCENE_PATH.c_str());
79 printf("%s\n",ExecuteCommand(command).c_str());
81 system("cd bin/Release && ./Application");
82 #elif defined(DEBUG)
83 printf("%s\n",ExecuteCommand("cd lib && make config=debug").c_str());
84 printf("%s\n",ExecuteCommand("cd temp && make config=debug").c_str());
85 ExecuteCommand("mkdir -p bin/Debug/resources");
86 ExecuteCommand("cp -r resources/* bin/Debug/resources");
87 char command[256];
88 command[0]='\0';
89 strcat(command,"cp ");
90 strcat(command,SCENE_PATH.c_str());
91 strcat(command," bin/Debug/");
92 strcat(command,SCENE_PATH.c_str());
93 printf("%s\n",ExecuteCommand(command).c_str());
95 system("cd bin/Debug && ./Application");
96 #elif defined(ENABLE_PROFILING)
97 printf("%s\n",ExecuteCommand("cd lib && make config=profile").c_str());
98 printf("%s\n",ExecuteCommand("cd temp && make config=profile").c_str());
99 ExecuteCommand("mkdir -p bin/Profile/resources");
100 ExecuteCommand("cp -r resources/* bin/Profile/resources");
101 char command[256];
102 command[0]='\0';
103 strcat(command,"cp ");
104 strcat(command,SCENE_PATH.c_str());
105 strcat(command," bin/Profile/");
106 strcat(command,SCENE_PATH.c_str());
107 printf("%s\n",ExecuteCommand(command).c_str());
109 system("cd bin/Profile && ./Application");
110 #endif
111
112 isApplicationRunning.store(false);
113 });
114 applicationThread.detach(); //detach the thread
115}
unsigned int FULLSCREEN_WIDTH
Definition editor.cpp:19
const char * WINDOW_NAME
Definition editor.cpp:16
unsigned int WINDOW_HEIGHT
Definition editor.cpp:18
unsigned int WINDOW_WIDTH
Definition editor.cpp:17
bool RESIZABLE
Definition editor.cpp:22
std::string SCENE_PATH
Definition editor.cpp:21
unsigned int FULLSCREEN_HEIGHT
Definition editor.cpp:20
void CreateShaders()
Definition renderer.cpp:171
TonemapType GetTonemapType()
Definition renderer.cpp:234
float GetGamma()
Definition renderer.cpp:242
float GetExposure()
Definition renderer.cpp:250
static void PlayButton(std::vector< std::pair< std::string, uint32_t > > &script_components)
TonemapType
Definition renderer.hpp:26
std::string TonemapTypeToString(TonemapType type)
Definition renderer.cpp:127
void GenerateApplication(std::vector< std::pair< std::string, uint32_t > > &script_components, std::string &window_name, unsigned int width, unsigned int height, unsigned int fullscreen_width, unsigned int fullscreen_height, std::string &scene_path, TonemapType tonemap, float gamma, float exposure, bool resizable=false)
fclose(fkey)
int i
std::string ExecuteCommand(const std::string &command)
Definition utilities.cpp:17
Renderer * RENDERER
The main renderer.
Definition window.cpp:12