-
Notifications
You must be signed in to change notification settings - Fork 871
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Take automatic game screenshots in Quick Customization #7116
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cc815a5
Taking a screenshot in the QC preview works locally
ClementPasteau 3f66fb9
Handle preview screenshots on web
ClementPasteau f31342d
Fix log
ClementPasteau 6a10c44
Implement feedback
ClementPasteau 94ada62
Last fixes
ClementPasteau ece935b
Last fixes
ClementPasteau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* GDevelop Core | ||
* Copyright 2008-2016 Florian Rival ([email protected]). All rights | ||
* reserved. This project is released under the MIT License. | ||
*/ | ||
#include "GDCore/IDE/CaptureOptions.h" | ||
|
||
#include "GDCore/String.h" | ||
|
||
using namespace std; | ||
|
||
namespace gd { | ||
|
||
Screenshot::Screenshot() {} | ||
|
||
CaptureOptions::CaptureOptions() {} | ||
|
||
} // namespace gd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#pragma once | ||
#include <memory> | ||
#include <vector> | ||
|
||
#include "GDCore/String.h" | ||
|
||
namespace gd { | ||
|
||
class GD_CORE_API Screenshot { | ||
public: | ||
Screenshot(); | ||
virtual ~Screenshot() {}; | ||
|
||
void SetDelayTimeInSeconds(int delayTimeInMs_) { | ||
delayTimeInMs = delayTimeInMs_; | ||
} | ||
const long GetDelayTimeInSeconds() const { return delayTimeInMs; } | ||
ClementPasteau marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
void SetSignedUrl(const gd::String& signedUrl_) { signedUrl = signedUrl_; } | ||
const gd::String& GetSignedUrl() const { return signedUrl; } | ||
|
||
void SetPublicUrl(const gd::String& publicUrl_) { publicUrl = publicUrl_; } | ||
const gd::String& GetPublicUrl() const { return publicUrl; } | ||
|
||
private: | ||
int delayTimeInMs = 0; | ||
gd::String signedUrl; | ||
gd::String publicUrl; | ||
}; | ||
|
||
class GD_CORE_API CaptureOptions { | ||
public: | ||
CaptureOptions(); | ||
virtual ~CaptureOptions() {}; | ||
|
||
bool IsEmpty() const { return screenshots.empty(); } | ||
|
||
void AddScreenshot(const Screenshot& screenshot) { | ||
screenshots.push_back(screenshot); | ||
} | ||
|
||
const std::vector<Screenshot>& GetScreenshots() const { return screenshots; } | ||
|
||
void ClearScreenshots() { screenshots.clear(); } | ||
|
||
private: | ||
std::vector<Screenshot> screenshots; | ||
}; | ||
|
||
} // namespace gd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* GDevelop JS Platform | ||
* Copyright 2013-2016 Florian Rival ([email protected]). All rights reserved. | ||
* This project is released under the MIT License. | ||
*/ | ||
namespace gdjs { | ||
export type CaptureOptions = { | ||
screenshots?: { | ||
delayTimeInSeconds: number; | ||
signedUrl: string; | ||
publicUrl: string; | ||
}[]; | ||
ClementPasteau marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
/** | ||
* Helper function to convert a base64 string to a Blob, which can be uploaded to a server. | ||
*/ | ||
const base64ToBlob = (base64) => { | ||
const byteString = atob(base64.split(',')[1]); | ||
const mimeString = base64.split(',')[0].split(':')[1].split(';')[0]; | ||
|
||
const arrayBuffer = new Uint8Array(byteString.length); | ||
for (let i = 0; i < byteString.length; i++) { | ||
arrayBuffer[i] = byteString.charCodeAt(i); | ||
} | ||
|
||
return new Blob([arrayBuffer], { type: mimeString }); | ||
}; | ||
|
||
/** | ||
* Manage the captures (screenshots, videos, etc...) that need to be taken during the game. | ||
*/ | ||
export class CaptureManager { | ||
_gameRenderer: gdjs.RuntimeGameRenderer; | ||
_captureOptions: CaptureOptions; | ||
|
||
constructor( | ||
renderer: gdjs.RuntimeGameRenderer, | ||
captureOptions: CaptureOptions | ||
) { | ||
this._gameRenderer = renderer; | ||
this._captureOptions = captureOptions; | ||
} | ||
|
||
/** | ||
* To be called when the scene has started rendering. | ||
*/ | ||
setupCaptureOptions(isPreview: boolean): void { | ||
if (!isPreview || !this._captureOptions.screenshots) { | ||
return; | ||
} | ||
|
||
for (const screenshotCaptureOption of this._captureOptions.screenshots) { | ||
setTimeout(async () => { | ||
await this.takeAndUploadScreenshot(screenshotCaptureOption.signedUrl); | ||
}, screenshotCaptureOption.delayTimeInSeconds); | ||
} | ||
} | ||
|
||
/** | ||
* Take a screenshot and upload it to the server. | ||
*/ | ||
async takeAndUploadScreenshot(signedUrl: string) { | ||
const canvas = this._gameRenderer.getCanvas(); | ||
if (canvas) { | ||
try { | ||
const base64Data = canvas.toDataURL('image/png'); | ||
const blobData = base64ToBlob(base64Data); | ||
|
||
await fetch(signedUrl, { | ||
method: 'PUT', | ||
body: blobData, | ||
headers: { | ||
'Content-Type': 'image/png', | ||
}, | ||
}); | ||
} catch (error) { | ||
console.error('Error while uploading screenshot:', error); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.