Skip to content

Commit

Permalink
clean the old score widget
Browse files Browse the repository at this point in the history
  • Loading branch information
fmang committed Jan 20, 2018
1 parent 4c0cc1f commit 66793c8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 110 deletions.
97 changes: 0 additions & 97 deletions src/ui/score.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,106 +9,9 @@

#include "beatmap/beatmap.h"
#include "graphics/display.h"
#include "graphics/paint.h"
#include "graphics/texture.h"

#include <assert.h>
#include <string.h>
#include <SDL2/SDL.h>

/**
* Estimate the duration of a beatmap from the timing information of its last
* hit object.
*/
double beatmap_duration(struct oshu_beatmap *beatmap)
{
struct oshu_hit *hit = beatmap->hits;
while (hit->next)
hit = hit->next;
return oshu_hit_end_time(hit->previous);
}

static int paint(struct oshu_score_widget *widget)
{
oshu_size size = 100 + 600 * I;
struct oshu_painter p;
oshu_start_painting(widget->display, size, &p);
cairo_translate(p.cr, creal(size) / 2., 0);

cairo_set_source_rgba(p.cr, 0, 0, 0, .6);
cairo_set_line_width(p.cr, 2);
cairo_move_to(p.cr, 0, 0);
cairo_line_to(p.cr, 0, cimag(size));
cairo_stroke(p.cr);

double duration = beatmap_duration(widget->beatmap);
double leniency = widget->beatmap->difficulty.leniency;
assert (leniency > 0);
assert (duration > 0);

cairo_set_source_rgba(p.cr, 1, 0, 0, .6);
cairo_set_line_width(p.cr, 1);
for (struct oshu_hit *hit = widget->beatmap->hits; hit; hit = hit->next) {
if (hit->state == OSHU_MISSED_HIT) {
double y = hit->time / duration * cimag(size);
cairo_move_to(p.cr, -10, y);
cairo_line_to(p.cr, 10, y);
cairo_stroke(p.cr);
}
}

cairo_set_source_rgba(p.cr, 1, 1, 0, .6);
cairo_set_line_width(p.cr, 1);
cairo_move_to(p.cr, 0, 0);
for (struct oshu_hit *hit = widget->beatmap->hits; hit; hit = hit->next) {
if (hit->offset < 0) {
double y = hit->time / duration * cimag(size);
double x = hit->offset / leniency * creal(size) / 2;
cairo_line_to(p.cr, x, y);
}
}
cairo_line_to(p.cr, 0, cimag(size));
cairo_stroke(p.cr);

cairo_set_source_rgba(p.cr, 1, 0, 1, .6);
cairo_set_line_width(p.cr, 1);
cairo_move_to(p.cr, 0, 0);
for (struct oshu_hit *hit = widget->beatmap->hits; hit; hit = hit->next) {
if (hit->offset > 0) {
double y = hit->time / duration * cimag(size);
double x = hit->offset / leniency * creal(size) / 2;
cairo_line_to(p.cr, x, y);
}
}
cairo_line_to(p.cr, 0, cimag(size));
cairo_stroke(p.cr);

struct oshu_texture *texture = &widget->offset_graph;
int rc = oshu_finish_painting(&p, texture);
texture->origin = size / 2.;
return rc;
}

int oshu_create_score_widget(struct oshu_display *display, struct oshu_beatmap *beatmap, struct oshu_score_widget *widget)
{
memset(widget, 0, sizeof(*widget));
widget->display = display;
widget->beatmap = beatmap;
return paint(widget);
}

void oshu_show_score_widget(struct oshu_score_widget *widget)
{
double x = creal(widget->display->view.size) - creal(widget->offset_graph.size);
double y = cimag(widget->display->view.size) / 2;
oshu_draw_texture(widget->display, &widget->offset_graph, x + y * I);
}

void oshu_destroy_score_widget(struct oshu_score_widget *widget)
{
oshu_destroy_texture(&widget->offset_graph);
}

int oshu_create_score_frame(struct oshu_display *display, struct oshu_beatmap *beatmap, struct oshu_score_frame *frame)
{
memset(frame, 0, sizeof(*frame));
Expand Down
38 changes: 25 additions & 13 deletions src/ui/score.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

#pragma once

#include "graphics/texture.h"

struct oshu_beatmap;
struct oshu_display;

/**
Expand All @@ -16,30 +15,43 @@ struct oshu_display;
* \brief
* Show the game score and statistics.
*
* This widget is under construction. [Insert animated GIF from the 90s]
* It's currently extremly simple, showing a bar filled with green for good
* notes, and red for bad notes.
*
* \{
*/

struct oshu_score_widget {
struct oshu_display *display;
struct oshu_beatmap *beatmap;
struct oshu_texture offset_graph;
};

int oshu_create_score_widget(struct oshu_display *display, struct oshu_beatmap *beatmap, struct oshu_score_widget *widget);
void oshu_show_score_widget(struct oshu_score_widget *widget);
void oshu_destroy_score_widget(struct oshu_score_widget *widget);

struct oshu_score_frame {
struct oshu_display *display;
struct oshu_beatmap *beatmap;
int good;
int bad;
};

/**
* Create the frame and compute the score.
*
* The score is computed once and for all when this function is called.
*
* Maybe in a future version, this widget could be permanently shown and
* updated on every action.
*/
int oshu_create_score_frame(struct oshu_display *display, struct oshu_beatmap *beatmap, struct oshu_score_frame *frame);

/**
* Show the score frame.
*
* It's a simple bar, centered in the bottom of the screen.
*
* The opacity argument lets you fade in the bar with #oshu_fade_in.
*/
void oshu_show_score_frame(struct oshu_score_frame *frame, double opacity);

/**
* Destroy a score frame.
*
* In fact, it does nothing, but is there for consistency.
*/
void oshu_destroy_score_frame(struct oshu_score_frame *frame);

/** \} */

0 comments on commit 66793c8

Please sign in to comment.