}
void TextManager::Render(char* text, int x, int y) {
- if (text == NULL || fonts[current_font] == NULL)
+ if (text == NULL || fonts[current_font] == NULL) {
+ fprintf(stderr, "[WARN] Null text or font passed.\n");
return;
+ }
+
SDL_Surface *sf1 = NULL, *sf2 = NULL;
+
sf1 = TTF_RenderUTF8_Blended(fonts[current_font], text, color);
+
if(outline) {
+ // Invert normal color.
SDL_Color n_color;
n_color.r = 255 - color.r;
n_color.g = 255 - color.g;
n_color.b = 255 - color.b;
n_color.a = color.a;
+
+ // Outline, so increase size.
TTF_SetFontOutline(fonts[current_font], outline_px);
sf2 = TTF_RenderUTF8_Blended(fonts[current_font], text, n_color);
+
+ // Disable Outline.
TTF_SetFontOutline(fonts[current_font], 0);
}
- SDL_Rect src, dst, src2, dst2;
- src.x = 0;
- src.y = 0;
- src.w = sf1->w;
- src.h = sf1->h;
-
- dst.x = x;
- dst.y = y;
- dst.w = src.w;
- dst.h = src.h;
-
if(outline) {
- src2.x = 0;
- src2.y = 0;
- src2.w = sf2->w;
- src2.h = sf2->h;
-
- dst2.x = x - (outline_px);
- dst2.y = y - (outline_px);
- dst2.w = src2.w;
- dst2.h = src2.h;
- }
-
- if(ctx->Accelerated()) {
- SDL_Texture *tmp1 = NULL, *tmp2 = NULL;
-
- tmp1 = SDL_CreateTextureFromSurface(ctx->Renderer(), sf1);
- if(outline)
- tmp2 = SDL_CreateTextureFromSurface(ctx->Renderer(), sf2);
-
- ctx->OverlayBlit(tmp2, &src2, &dst2, NULL);
- ctx->OverlayBlit(tmp1, &src, &dst, NULL);
+ UDisplayable* r_sf2 = new UDisplayable(ctx, Normal, sf2);
+ r_sf2->SetOverlay(true);
+ r_sf2->SetXY(x-outline_px, y-outline_px);
- SDL_DestroyTexture(tmp2);
- SDL_DestroyTexture(tmp1);
-
- }
- else {
- ctx->OverlayBlit(sf2, &src, &dst, NULL);
- ctx->OverlayBlit(sf1, &src, &dst, NULL);
+ r_sf2->Blit();
+
+ delete r_sf2;
}
- SDL_FreeSurface(sf2);
- SDL_FreeSurface(sf1);
+ UDisplayable* r_sf1 = new UDisplayable(ctx, Normal, sf1);
+ r_sf1->SetOverlay(true);
+ r_sf1->SetXY(x, y);
+ r_sf1->Blit();
+ delete r_sf1;
}
int TextManager::TestLen(char* text) {
// So your string will likely not be usable as before.
void TextManager::SplitStringByWidth(char* string, int max_w, int* OUT_num, char*** OUT_ptrs) {
if(TestLen(string) > max_w) {
+ /* new algo */
+ char** ptrs = NULL;
+ int lines = 0;
+ int len = strlen(string);
- /* new algo */
- char** ptrs = NULL;
- int lines = 0;
+ int counted = 0;
- int len = strlen(string);
+ while(counted < len) {
+ char* pt_start = &string[counted];
+ char* pt_end = &pt_start[strlen(pt_start)];
- int counted = 0;
+ while(pt_end > pt_start && TestLen(pt_start) > max_w) {
+ *pt_end = ' ';
+ --pt_end;
+ while (*pt_end != ' ' && pt_end > pt_start) --pt_end;
- while(counted < len) {
- char* pt_start = &string[counted];
- char* pt_end = &pt_start[strlen(pt_start)];
-
- while(pt_end > pt_start && TestLen(pt_start) > max_w) {
- *pt_end = ' ';
- --pt_end;
-
- while (*pt_end != ' ' && pt_end > pt_start) --pt_end;
+ *pt_end = '\0';
+ }
- *pt_end = '\0';
- }
+ #ifdef DEBUG_OVERKILL
+ printf("Reduced line %d: %s\n", lines, pt_start);
+ #endif
- #ifdef DEBUG_OVERKILL
- printf("Reduced line %d: %s\n", lines, pt_start);
- #endif
+ ptrs = (char**)realloc(ptrs, sizeof(char*)*(lines+1));
- ptrs = (char**)realloc(ptrs, sizeof(char*)*(lines+1));
+ ptrs[lines] = pt_start;
- ptrs[lines] = pt_start;
+ counted += strlen(pt_start) + 1;
- counted += strlen(pt_start) + 1;
+ ++lines;
+ }
- ++lines;
- }
+ *OUT_num = lines;
- OUT_num[0] = lines;
+ *OUT_ptrs = ptrs;
+ }
+ else {
+ char** ptrs = (char**)calloc(sizeof(char*), 1);
+ ptrs[0] = string;
- OUT_ptrs[0] = ptrs;
- }
+ // We pass thru values anyways, regardless of there being one line.
+ *OUT_num = 1;
+ *OUT_ptrs = ptrs;
+ }
}
// From SDL_Surface.
UDisplayable::UDisplayable(ContextManager* cx, UDisplayableMode mode, SDL_Surface* bitmap_tmp) {
+
+ DefaultVars();
+
this->x = 0;
this->y = 0;
this->loc.x = 0;
// Sets the position on screen.
void UDisplayable::SetXY(double x, double y) {
- if (Error)
+ if (Error) {
+ fprintf(stderr, "[WARN] Error flag set.\n");
return;
+ }
this->x = x;
this->y = y;
this->y = 0;
if(frameIndex == -1) {
- if(this->x > frame[2] - this->bmp_w)
- this->x = (double)(frame[2] - this->bmp_w);
+ if(over) {
+ if(this->x > ctx->GetWidth() - this->bmp_w)
+ this->x = (double)(ctx->GetWidth() - this->bmp_w);
+ }
+ else {
+ if(this->x > frame[2] - this->bmp_w)
+ this->x = (double)(frame[2] - this->bmp_w);
+ }
}
else {
- if(this->x > frame[2] - this->frameWidth)
- this->x = (double)(frame[2] - this->frameWidth);
+ if(over) {
+ if(this->x > ctx->GetWidth() - this->bmp_w)
+ this->x = (double)(ctx->GetWidth() - this->frameWidth);
+ }
+ else {
+ if(this->x > frame[2] - this->frameWidth)
+ this->x = (double)(frame[2] - this->frameWidth);
+ }
}
- if(this->y > frame[3] - this->bmp_h)
- this->y = (double)(frame[3] - this->bmp_h);
+ if(over) {
+ if(this->y > ctx->GetHeight() - this->bmp_h)
+ this->y = (double)(ctx->GetHeight() - this->bmp_h);
+ }
+ else {
+ if(this->y > frame[3] - this->bmp_h)
+ this->y = (double)(frame[3] - this->bmp_h);
+ }
this->loc.x = (int)this->x;
this->loc.y = (int)this->y;
- #ifdef DEBUG_OVERKILL
- printf("[UDisplayable::SetXY] x:%d y:%d w:%d h:%d\n", frame[0], frame[1], frame[2], frame[3]);
- #endif
+// #ifdef DEBUG_OVERKILL
+ printf("[UDisplayable::SetXY] x:%d y:%d\n", this->loc.x, this->loc.y);
+// #endif
}
// Modifies the position on screen. Meant to avoid embedded retrievals.
this->y = 0;
if(frameIndex == -1) {
- if(this->x > frame[2] - this->bmp_w)
- this->x = (double)(frame[2] - this->bmp_w);
+ if(over) {
+ if(this->x > ctx->GetWidth() - this->bmp_w)
+ this->x = (double)(ctx->GetWidth() - this->bmp_w);
+ }
+ else {
+ if(this->x > frame[2] - this->bmp_w)
+ this->x = (double)(frame[2] - this->bmp_w);
+ }
}
else {
- if(this->x > frame[2] - this->frameWidth)
- this->x = (double)(frame[2] - this->frameWidth);
+ if(over) {
+ if(this->x > ctx->GetWidth() - this->bmp_w)
+ this->x = (double)(ctx->GetWidth() - this->frameWidth);
+ }
+ else {
+ if(this->x > frame[2] - this->frameWidth)
+ this->x = (double)(frame[2] - this->frameWidth);
+ }
}
- if(this->y > frame[3] - this->bmp_h)
- this->y = (double)(frame[3] - this->bmp_h);
+ if(over) {
+ if(this->y > ctx->GetHeight() - this->bmp_h)
+ this->y = (double)(ctx->GetHeight() - this->bmp_h);
+ }
+ else {
+ if(this->y > frame[3] - this->bmp_h)
+ this->y = (double)(frame[3] - this->bmp_h);
+ }
this->loc.x = (int)this->x;
this->loc.y = (int)this->y;
SDL_Rect src;
src.x = 0;
src.y = 0;
- src.w = frameWidth;
+ src.w = bmp_w;
src.h = bmp_h;
if (frameIndex == -1) {
frameClip.h = bmp_h;
if(ctx->GLMode()) {
- SDL_Rect image_rect;
-
- image_rect.x = 0;
- image_rect.x = 0;
- image_rect.w = bmp_w;
- image_rect.h = bmp_h;
if(over)
- ctx->OverlayBlit(bitmap, &frameClip, &loc_adj, &image_rect); // GL needs data that isn't inside of bitmap.
+ ctx->OverlayBlit(bitmap, &frameClip, &loc_adj, &src); // GL needs data that isn't inside of bitmap.
else
- ctx->Blit(bitmap, &frameClip, &loc_adj, &image_rect); // GL needs data that isn't inside of bitmap.
+ ctx->Blit(bitmap, &frameClip, &loc_adj, &src); // GL needs data that isn't inside of bitmap.
}
else {
// Dump script file.
// A save actually ends up as a mini-script.
- // So, to load a save you load this as the
- // main script
- // For example:
+ // So, to load a save you load something like this as the
+ // main script:
+
// setvar data1 = 6
// setvar data2 = 9
// music mus
// bgload bg
// jump script.scr 60
- // The call command runs a scr, and returns here.
FILE* save_to = fopen(fname, "w");
fprintf(save_to, "music %s\n", &(GetData()->current_music[6]));
fprintf(save_to, "bgload %s\n", &(GetData()->current_bg[11]));
- fprintf(save_to, "jump %s %d\n", &(GetData()->current_scr[7]), GetData()->currentLine - 1); // The text never completely displayed
- // So restore back one to replay
-
+ fprintf(save_to, "jump %s %d\n", &(GetData()->current_scr[7]), GetData()->currentLine - 1); // The text ack'd with leftclick
+ // So restore back one to replay
fclose(save_to);
}
if(GetData()->ctx->GetQuit()) return;
Parse();
- // We don't clear. This system uses dirty blits. ;P
- GetData()->ctx->Flush();
+ // We don't clear. This system uses dirty blits. ;P
+ GetData()->ctx->Flush();
GetData()->ctx->StartSync();
if (GetData()->if_fail != 0 || GetData()->ctx->GetQuit())
return;
- // Fadeout not implemented yet.
-
memset(GetData()->current_bg, 0, 400);
snprintf(GetData()->current_bg, 400, "background/%s", file);
- //printf("Attempt to load file %s as BG\n", path);
-
// Load displayable.
UDisplayable* disp = new UDisplayable(GetData()->ctx, GetData()->current_bg);
if(fadetime != NULL)
transp_incr = 255 / *fadetime;
- // Transition effect.
-
- // SDL_Texture* tx = NULL;
- // tx = SDL_CreateTextureFromSurface(GetData()->ctx->Renderer(), sfc);
- // SDL_SetTextureBlendMode(tx, SDL_BLENDMODE_BLEND);
-
- // SDL_FreeSurface(sfc);
+ // FIXME - Implement proper fading out functionality.
int delay = 1;
for(int tr = 0; tr < 255; tr += transp_incr) {
disp->Blit();
op_delay(&delay);
}
-
-// SDL_DestroyTexture(tx);
}
if (GetData()->if_fail != 0 || GetData()->ctx->GetQuit())
return;
- // Search thru for vars and rebuild string.
-
// Linebreak on zero-length, and render size exceed.
if (GetData()->text_y > (GetData()->render_y2) || strlen(string) < 1)
op_cleartext();
TextManager* txt = GetData()->ctx->Text();
- if(txt->TestLen(string) > (GetData()->render_x2 - GetData()->render_x1)) {
-
- /* new algo */
- char** ptrs = NULL;
- int lines = 0;
-
- int len = strlen(string);
-
- int counted = 0;
-
- while(counted < len) {
- char* pt_start = &string[counted];
- char* pt_end = &pt_start[strlen(pt_start)];
-
- while(pt_end > pt_start && txt->TestLen(pt_start) > (GetData()->render_x2 - GetData()->render_x1)) {
- *pt_end = ' ';
- --pt_end;
-
- while (*pt_end != ' ' && pt_end > pt_start) --pt_end;
-
- *pt_end = '\0';
- }
+ int lines = 0;
+ char** ptrs = NULL;
- #ifdef DEBUG_OVERKILL
- printf("Reduced line %d: %s\n", lines, pt_start);
- #endif
+ txt->SplitStringByWidth(string, GetData()->render_x2 - GetData()->render_x1, &lines, &ptrs);
- ptrs = (char**)realloc(ptrs, sizeof(char*)*(lines+1));
+ if( ( lines * GetData()->text_gap + GetData()->text_y ) > GetData()->render_y2 )
+ op_cleartext();
- ptrs[lines] = pt_start;
-
- counted += strlen(pt_start) + 1;
-
- ++lines;
- }
-
- if( ( lines * GetData()->text_gap + GetData()->text_y ) > GetData()->render_y2 )
- op_cleartext();
-
- for(int i=0; i < lines; i++) {
- //printf("[br] %s\n", ptrs[i]);
- txt->Render(ptrs[i], GetData()->text_x, GetData()->text_y);
- GetData()->text_y += GetData()->text_gap;
- }
-
- free(ptrs);
-
- }
- else {
- txt->Render(string, GetData()->text_x, GetData()->text_y);
+ for(int i=0; i < lines; i++) {
+ printf("[br] %s\n", ptrs[i]);
+ txt->Render(ptrs[i], GetData()->text_x, GetData()->text_y);
GetData()->text_y += GetData()->text_gap;
}
+ free(ptrs);
+
if(!noclick) {
GetData()->wait_input = true;
}