Permalink
Browse files

Fix crash if opening audio device fails, more work on porting launche…

…r to C
  • Loading branch information...
UnknownShadow200 committed Dec 1, 2018
1 parent 55a5008 commit a3b8cd48c20ba4f502b1c10f857f47f88e80876e
@@ -97,7 +97,7 @@ public sealed partial class AudioPlayer {
void PlaySound(IAudioOutput output, float volume) {
try {
output.SetVolume(volume);
output.SetFormat(format);
output.SetFormat(format);
output.PlayData(0, chunk);
} catch (InvalidOperationException ex) {
ErrorHandler.LogError("AudioPlayer.PlayCurrentSound()", ex);
@@ -94,6 +94,7 @@ public unsafe sealed class WinMmOut : IAudioOutput {
public override void Play() { }

public override void Stop() {
if (devHandle == IntPtr.Zero) return;
uint result = WinMM.waveOutReset(devHandle);
CheckError(result, "Reset");
}
@@ -105,7 +105,7 @@ static ReturnCode Sound_ReadWave(const String* filename, struct Sound* snd) {
ReturnCode res;

String_InitArray(path, pathBuffer);
String_Format2(&path, "audio%r%s", &Directory_Separator, filename);
String_Format1(&path, "audio/%s", filename);

res = Stream_OpenFile(&stream, &path);
if (res) return res;
@@ -460,7 +460,7 @@ static void Music_RunLoop(void) {
file = StringsBuffer_UNSAFE_Get(&files, musicFiles[idx]);

path.length = 0;
String_Format2(&path, "audio%r%s", &Directory_Separator, &file);
String_Format1(&path, "audio/%s", &file);
Platform_Log1("playing music file: %s", &file);

res = Stream_OpenFile(&stream, &path);
@@ -101,7 +101,7 @@ static void Chat_OpenLog(struct DateTime* now) {
/* Ensure multiple instances do not end up overwriting each other's log entries. */
for (i = 0; i < 20; i++) {
path->length = 0;
String_Format4(path, "logs%r%p4-%p2-%p2 ", &Directory_Separator, &now->Year, &now->Month, &now->Day);
String_Format3(path, "logs/%p4-%p2-%p2 ", &now->Year, &now->Month, &now->Day);

if (i > 0) {
String_Format2(path, "%s _%i.log", &Chat_LogName, &i);
@@ -210,6 +210,7 @@
<ClInclude Include="Gui.h" />
<ClInclude Include="HeldBlockRenderer.h" />
<ClInclude Include="Launcher.h" />
<ClInclude Include="LWidgets.h" />
<ClInclude Include="Model.h" />
<ClInclude Include="Input.h" />
<ClInclude Include="InputHandler.h" />
@@ -226,6 +227,7 @@
<ClInclude Include="BlockPhysics.h" />
<ClInclude Include="Picking.h" />
<ClInclude Include="PickedPosRenderer.h" />
<ClInclude Include="Resources.h" />
<ClInclude Include="Screens.h" />
<ClInclude Include="SelectionBox.h" />
<ClInclude Include="ServerConnection.h" />
@@ -271,6 +273,7 @@
<ClCompile Include="InputHandler.c" />
<ClCompile Include="Inventory.c" />
<ClCompile Include="Launcher.c" />
<ClCompile Include="LWidgets.c" />
<ClCompile Include="MapGenerator.c" />
<ClCompile Include="Deflate.c" />
<ClCompile Include="Model.c" />
@@ -290,6 +293,7 @@
<ClCompile Include="Picking.c" />
<ClCompile Include="Platform.c" />
<ClCompile Include="Program.c" />
<ClCompile Include="Resources.c" />
<ClCompile Include="Screens.c" />
<ClCompile Include="SelectionBox.c" />
<ClCompile Include="ServerConnection.c" />
@@ -306,6 +306,12 @@
<ClInclude Include="Launcher.h">
<Filter>Header Files\Launcher</Filter>
</ClInclude>
<ClInclude Include="Resources.h">
<Filter>Header Files\Launcher</Filter>
</ClInclude>
<ClInclude Include="LWidgets.h">
<Filter>Header Files\Launcher</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="String.c">
@@ -545,5 +551,11 @@
<ClCompile Include="Launcher.c">
<Filter>Source Files\Launcher</Filter>
</ClCompile>
<ClCompile Include="Resources.c">
<Filter>Source Files\Launcher</Filter>
</ClCompile>
<ClCompile Include="LWidgets.c">
<Filter>Source Files\Launcher</Filter>
</ClCompile>
</ItemGroup>
</Project>
@@ -95,8 +95,8 @@ bool Drawer2D_Clamp(Bitmap* bmp, int* x, int* y, int* width, int* height) {
}
#define Drawer2D_ClampPixel(p) (p < 0 ? 0 : (p > 255 ? 255 : p))

void Gradient_Noise(Bitmap* bmp, int x, int y, int width, int height,
BitmapCol col, int variation) {
void Gradient_Noise(Bitmap* bmp, BitmapCol col, int variation,
int x, int y, int width, int height) {
BitmapCol* dst;
int xx, yy, n;
float noise;
@@ -122,8 +122,8 @@ void Gradient_Noise(Bitmap* bmp, int x, int y, int width, int height,
}
}

void Gradient_Vertical(Bitmap* bmp, int x, int y, int width, int height,
PackedCol a, PackedCol b) {
void Gradient_Vertical(Bitmap* bmp, BitmapCol a, BitmapCol b,
int x, int y, int width, int height) {
BitmapCol* row, col;
int xx, yy;
float t;
@@ -142,8 +142,8 @@ void Gradient_Vertical(Bitmap* bmp, int x, int y, int width, int height,
}
}

void Gradient_Blend(Bitmap* bmp, int x, int y, int width, int height,
PackedCol col, int blend) {
void Gradient_Blend(Bitmap* bmp, BitmapCol col, int blend,
int x, int y, int width, int height) {
BitmapCol* dst;
int xx, yy, t;
if (!Drawer2D_Clamp(bmp, &x, &y, &width, &height)) return;
@@ -30,12 +30,12 @@ extern BitmapCol Drawer2D_Cols[DRAWER2D_MAX_COLS];
/* Returns false if rectangle is completely outside bitmap's rectangle. */
bool Drawer2D_Clamp(Bitmap* bmp, int* x, int* y, int* width, int* height);

void Gradient_Noise(Bitmap* bmp, int x, int y, int width, int height,
BitmapCol col, int variation);
void Gradient_Vertical(Bitmap* bmp, int x, int y, int width, int height,
PackedCol a, PackedCol b);
void Gradient_Blend(Bitmap* bmp, int x, int y, int width, int height,
PackedCol col, int blend);
void Gradient_Noise(Bitmap* bmp, BitmapCol col, int variation,
int x, int y, int width, int height);
void Gradient_Vertical(Bitmap* bmp, BitmapCol a, BitmapCol b,
int x, int y, int width, int height);
void Gradient_Blend(Bitmap* bmp, BitmapCol col, int blend,
int x, int y, int width, int height);

void Drawer2D_BmpIndexed(Bitmap* bmp, int x, int y, int size,
uint8_t* indices, BitmapCol* palette);
@@ -894,10 +894,9 @@ static void EnvRenderer_EnvVariableChanged(void* obj, int envVar) {
*--------------------------------------------------EnvRenderer component--------------------------------------------------*
*#########################################################################################################################*/
static void EnvRenderer_Init(void) {
String renderType; char renderTypeBuffer[STRING_SIZE];
String renderType;
int flags;
String_InitArray(renderType, renderTypeBuffer);
Options_Get(OPT_RENDER_TYPE, &renderType, "normal");
Options_UNSAFE_Get(OPT_RENDER_TYPE, &renderType);

flags = Game_CalcRenderType(&renderType);
if (flags == -1) flags = 0;
@@ -130,7 +130,7 @@ void Game_GetDefaultTexturePack(String* texPack) {
String texPath; char texPathBuffer[STRING_SIZE];

String_InitArray(texPath, texPathBuffer);
String_Format2(&texPath, "texpacks%r%s", &Directory_Separator, &game_defTexPack);
String_Format1(&texPath, "texpacks/%s", &game_defTexPack);

if (File_Exists(&texPath) && !Game_ClassicMode) {
String_AppendString(texPack, &game_defTexPack);
@@ -421,7 +421,7 @@ static void Game_LoadGuiOptions(void) {

Game_TabAutocomplete = Options_GetBool(OPT_TAB_AUTOCOMPLETE, false);
Options_Get(OPT_FONT_NAME, &Game_FontName, Font_DefaultName);
if (Game_ClassicMode) {
if (!Game_ClassicMode) {
Game_FontName.length = 0;
String_AppendConst(&Game_FontName, Font_DefaultName);
}
@@ -641,7 +641,7 @@ void Game_TakeScreenshot(void) {
String_Format3(&filename, "screenshot_%p2-%p2-%p4", &now.Day, &now.Month, &now.Year);
String_Format3(&filename, "-%p2-%p2-%p2.png", &now.Hour, &now.Minute, &now.Second);
String_InitArray(path, pathBuffer);
String_Format2(&path, "screenshots%r%s", &Directory_Separator, &filename);
String_Format1(&path, "screenshots/%s", &filename);

res = Stream_CreateFile(&stream, &path);
if (res) { Chat_LogError2(res, "creating", &path); return; }
@@ -74,7 +74,7 @@ void InputHandler_ScreenChanged(struct Screen* oldScreen, struct Screen* newScre
static bool InputHandler_IsShutdown(Key key) {
if (key == KEY_F4 && Key_IsAltPressed()) return true;

/* On OSX, Cmd+Q should also terminate the process. */
/* On OSX, Cmd+Q should also terminate the process */
#ifdef CC_BUILD_OSX
return key == KEY_Q && Key_IsWinPressed();
#else
@@ -0,0 +1,181 @@
#include "LWidgets.h"
#include "Gui.h"
#include "Game.h"
#include "Drawer2D.h"
#include "Launcher.h"
#include "ExtMath.h"

void LWidget_SetLocation(void* widget, uint8_t horAnchor, uint8_t verAnchor, int xOffset, int yOffset) {
struct Widget* w = widget;
w->HorAnchor = horAnchor; w->VerAnchor = verAnchor;
w->XOffset = xOffset; w->YOffset = yOffset;
LWidget_CalcPosition(widget);
}

void LWidget_CalcPosition(void* widget) {
struct LWidget* w = widget;
w->X = Gui_CalcPos(w->HorAnchor, w->XOffset, w->Width, Game_Width);
w->Y = Gui_CalcPos(w->VerAnchor, w->YOffset, w->Height, Game_Height);
}

void LWidget_Reset(void* widget) {
struct LWidget* w = widget;
w->Active = false;
w->Hidden = false;
w->X = 0; w->Y = 0;
w->Width = 0; w->Height = 0;
w->HorAnchor = ANCHOR_MIN;
w->VerAnchor = ANCHOR_MIN;
w->XOffset = 0; w->YOffset = 0;

w->TabSelectable = false;
w->OnClick = NULL;
w->Redraw = NULL;
}


/*########################################################################################################################*
*------------------------------------------------------ButtonWidget-------------------------------------------------------*
*#########################################################################################################################*/
#define BTN_BORDER 1
static BitmapCol Expand(BitmapCol a, int amount) {
int r, g, b;
r = a.R + amount; Math_Clamp(r, 0, 255); a.R = r;
g = a.G + amount; Math_Clamp(g, 0, 255); a.G = g;
b = a.B + amount; Math_Clamp(b, 0, 255); a.B = b;
return a;
}

static void LButton_DrawBackground(struct LButton* w) {
BitmapCol activeCol = BITMAPCOL_CONST(126, 136, 191, 255);
BitmapCol inactiveCol = BITMAPCOL_CONST(111, 111, 111, 255);
BitmapCol col;

if (Launcher_ClassicBackground) {
col = w->Active ? activeCol : inactiveCol;
Gradient_Noise(&Launcher_Framebuffer, col, 8,
w->X + BTN_BORDER, w->Y + BTN_BORDER,
w->Width - 2 * BTN_BORDER, w->Height - 2 * BTN_BORDER);
} else {
col = w->Active ? Launcher_ButtonForeActiveCol : Launcher_ButtonForeCol;
BitmapCol top = Expand(col, 8), bottom = Expand(col, -8);
Gradient_Vertical(&Launcher_Framebuffer, top, bottom,
w->X + BTN_BORDER, w->Y + BTN_BORDER,
w->Width - 2 * BTN_BORDER, w->Height - 2 * BTN_BORDER);
}
}

static void LButton_DrawBorder(struct LButton* w) {
BitmapCol black = BITMAPCOL_CONST(0, 0, 0, 255);
BitmapCol backCol = Launcher_ClassicBackground ? black : Launcher_ButtonBorderCol;

Drawer2D_Clear(&Launcher_Framebuffer, backCol,
w->X + BTN_BORDER, w->Y,
w->Width - 2 * BTN_BORDER, BTN_BORDER);
Drawer2D_Clear(&Launcher_Framebuffer, backCol,
w->X + BTN_BORDER, w->Y + w->Height - BTN_BORDER,
w->Width - 2 * BTN_BORDER, BTN_BORDER);
Drawer2D_Clear(&Launcher_Framebuffer, backCol,
w->X, w->Y + BTN_BORDER,
BTN_BORDER, w->Height - 2 * BTN_BORDER);
Drawer2D_Clear(&Launcher_Framebuffer, backCol,
w->X + w->Width - BTN_BORDER, w->Y + BTN_BORDER,
BTN_BORDER, w->Height - 2 * BTN_BORDER);
}

static void LButton_DrawHighlight(struct LButton* w) {
BitmapCol activeCol = BITMAPCOL_CONST(189, 198, 255, 255);
BitmapCol inactiveCol = BITMAPCOL_CONST(168, 168, 168, 255);
BitmapCol highlightCol;

if (Launcher_ClassicBackground) {
highlightCol = w->Active ? activeCol : inactiveCol;
Drawer2D_Clear(&Launcher_Framebuffer, highlightCol,
w->X + BTN_BORDER * 2, w->Y + BTN_BORDER,
w->Width - BTN_BORDER * 4, BTN_BORDER);
Drawer2D_Clear(&Launcher_Framebuffer, highlightCol,
w->X + BTN_BORDER, w->Y + BTN_BORDER * 2,
BTN_BORDER, w->Height - BTN_BORDER * 4);
} else if (!w->Active) {
Drawer2D_Clear(&Launcher_Framebuffer, Launcher_ButtonHighlightCol,
w->X + BTN_BORDER * 2, w->Y + BTN_BORDER,
w->Width - BTN_BORDER * 4, BTN_BORDER);
}
}

static void LButton_Redraw(void* widget) {
struct DrawTextArgs args;
struct LButton* w = widget;
int xOffset, yOffset;
if (w->Hidden) return;

xOffset = w->Width - w->__TextSize.Width;
yOffset = w->Height - w->__TextSize.Height;
DrawTextArgs_Make(&args, &w->Text, &w->Font, true);

LButton_DrawBackground(w);
LButton_DrawBorder(w);
LButton_DrawHighlight(w);

if (!w->Active) Drawer2D_Cols['f'] = Drawer2D_Cols['7'];
Drawer2D_DrawText(&Launcher_Framebuffer, &args,
w->X + xOffset / 2, w->Y + yOffset / 2);
if (!w->Active) Drawer2D_Cols['f'] = Drawer2D_Cols['F'];
}

void LButton_Init(struct LButton* w, int width, int height) {
Widget_Reset(w);
w->TabSelectable = true;
w->Width = width; w->Height = height;
w->Redraw = LButton_Redraw;
String_InitArray(w->Text, w->__TextBuffer);
}

void LButton_SetText(struct LButton* w, const String* text, const FontDesc* font) {
struct DrawTextArgs args;
w->Font = *font;
String_Copy(&w->Text, text);

DrawTextArgs_Make(&args, text, font, true);
w->__TextSize = Drawer2D_MeasureText(&args);
}


CC_NOINLINE void LInput_Init(struct LInput* w, int width, int height, const char* hintText, const FontDesc* hintFont);
CC_NOINLINE void LInput_SetText(struct LInput* w, const String* text, const FontDesc* font);


static void LLabel_Redraw(void* widget) {
struct DrawTextArgs args;
struct LLabel* w = widget;
if (w->Hidden) return;

DrawTextArgs_Make(&args, &w->Text, &w->Font, true);
Drawer2D_DrawText(&Launcher_Framebuffer, &args, w->X, w->Y);
}

void LLabel_Init(struct LLabel* w) {
Widget_Reset(w);
w->Redraw = LLabel_Redraw;
String_InitArray(w->Text, w->__TextBuffer);
}

void LLabel_SetText(struct LLabel* w, const String* text, const FontDesc* font) {
struct DrawTextArgs args;
Size2D size;
w->Font = *font;
String_Copy(&w->Text, text);

DrawTextArgs_Make(&args, &w->Text, &w->Font, true);
size = Drawer2D_MeasureText(&args);
w->Width = size.Width; w->Height = size.Height;
LWidget_CalcPosition(w);
}


static void LSlider_Redraw(void* widget) {
struct LSlider* w = widget;

}

void LSlider_Init(struct LSlider* w, int width, int height);
Oops, something went wrong.

0 comments on commit a3b8cd4

Please sign in to comment.