From: Carl Worth Date: Sat, 17 Sep 2011 03:41:17 +0000 (-0700) Subject: Start drawing something X-Git-Url: https://git.cworth.org/git?p=scherzo;a=commitdiff_plain;h=66812a216aa78f33ddff8753dd62f8450ef3220a Start drawing something So far, just a few simple staff lines. --- diff --git a/Makefile b/Makefile index 3bcf15a..3df2349 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,48 @@ -scherzo: scherzo.c - gcc -Wall -Wextra $$(pkg-config --cflags --libs gtk+-2.0) -o scherzo scherzo.c +all: scherzo +scherzo_srcs = \ + scherzo.c \ + score.c + +scherzo_modules = $(scherzo_srcs:.c=.o) + +WARN_CFLAGS = -Wall -Wextra + +SCHERZO_CFLAGS = $(CFLAGS) $(WARN_CFLAGS) $(CONFIGURE_CFLAGS) $(extra_cflags) `pkg-config --cflags gtk+-2.0` + +SCHERZO_LDFLAGS = $(LDFLAGS) `pkg-config --libs gtk+-2.0` + +# The user has not set any verbosity, default to quiet mode and inform the +# user how to enable verbose compiles. +ifeq ($(V),) +quiet_DOC := "Use \"$(MAKE) V=1\" to see the verbose compile lines.\n" +quiet = @printf $(quiet_DOC)$(eval quiet_DOC:=)"$1 $@\n"; $($(shell echo $1 | sed -e s'/ .*//')) +endif +# The user has explicitly enabled quiet compilation. +ifeq ($(V),0) +quiet = @printf "$1 $@\n"; $($(shell echo $1 | sed -e s'/ .*//')) +endif +# Otherwise, print the full command line. +quiet ?= $($(shell echo $1 | sed -e s'/ .*//')) + +.deps/%.d: %.cpp $(global_deps) + @set -e; rm -f $@; mkdir -p $$(dirname $@) ; \ + $(CXX) -M $(CPPFLAGS) $(FINAL_CXXFLAGS) $< > $@.$$$$ 2>/dev/null ; \ + sed 's,'$$(basename $*)'\.o[ :]*,$*.o $@ : ,g' < $@.$$$$ > $@; \ + rm -f $@.$$$$ + +DEPS := $(DEPS:%.cpp=.deps/%.d) +-include $(DEPS) + +%.o: %.c $(global_deps) + $(call quiet,CC $(CFLAGS)) -c $(SCHERZO_CFLAGS) $< -o $@ + +scherzo: $(scherzo_modules) + $(call quiet,CC $(CFLAGS)) $^ $(SCHERZO_LDFLAGS) -o $@ + +CLEAN := $(CLEAN) scherzo $(chorale_modules) + +.PHONY : clean clean: - rm -f scherzo + rm -f $(CLEAN); rm -rf .deps diff --git a/scherzo.c b/scherzo.c index 77db9bb..fbb23f0 100644 --- a/scherzo.c +++ b/scherzo.c @@ -18,6 +18,8 @@ #include +#include "score.h" + #define unused(foo) foo __attribute__((unused)) static int @@ -35,19 +37,29 @@ on_delete_event_quit (unused (GtkWidget *widget), static int on_expose_event_draw (GtkWidget *widget, unused (GdkEventExpose *event), - unused (gpointer user_data)) + void * user_data) { + score_t *score = user_data; cairo_t *cr; GtkAllocation allocation; + static const int pad = 10; + int widget_width, staff_width; gtk_widget_get_allocation (widget, &allocation); + widget_width = allocation.width; cr = gdk_cairo_create (widget->window); - /* Paint in medium sea green */ - cairo_set_source_rgb (cr, 60/255.0, 179/255.0, 113/255.0); - + /* White background */ + cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); cairo_paint (cr); + + /* Add some padding on the left/right */ + cairo_translate (cr, pad, pad); + + score_set_width (score, widget_width - 2 * pad); + + score_draw (score, cr); return TRUE; } @@ -57,9 +69,12 @@ main (int argc, char *argv[]) { GtkWidget *window; GtkWidget *drawing_area; + score_t score; gtk_init (&argc, &argv); + score_init (&score); + window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_window_set_default_size (GTK_WINDOW (window), 600, 400); @@ -72,8 +87,8 @@ main (int argc, char *argv[]) gtk_container_add (GTK_CONTAINER (window), drawing_area); g_signal_connect (drawing_area, "expose-event", - G_CALLBACK (on_expose_event_draw), NULL); - + G_CALLBACK (on_expose_event_draw), + &score); gtk_widget_show_all (window); diff --git a/score.c b/score.c new file mode 100644 index 0000000..4f0efdd --- /dev/null +++ b/score.c @@ -0,0 +1,59 @@ +/* scherzo - Music notation training + * + * score - Utilities for drawing (simple) musical scores + * + * Copyright © 2010 Carl Worth + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://www.gnu.org/licenses/ . + */ + +#include "score.h" + +void +score_init (score_t *score) +{ + score->space_height = 6; +} + +void +_draw_staff (score_t *score, cairo_t *cr) +{ + int i; + + cairo_save (cr); + + for (i = 0; i < 5; i++) { + cairo_move_to (cr, 0, i * score->space_height + 0.5); + cairo_rel_line_to (cr, score->width, 0); + } + + cairo_set_line_width (cr, 1.0); + + cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */ + cairo_stroke (cr); + + cairo_restore (cr); +} + +void +score_set_width (score_t *score, int width) +{ + score->width = width; +} + +void +score_draw (score_t *score, cairo_t *cr) +{ + _draw_staff (score, cr); +} diff --git a/score.h b/score.h new file mode 100644 index 0000000..26b9766 --- /dev/null +++ b/score.h @@ -0,0 +1,41 @@ +/* scherzo - Music notation training + * + * score - Utilities for drawing (simple) musical scores + * + * Copyright © 2010 Carl Worth + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://www.gnu.org/licenses/ . + */ + +#ifndef SCORE_H +#define SCORE_H + +#include + +typedef struct score +{ + /* Height of each space on staff. */ + int space_height; + + /* Total width available to score. */ + int width; +} score_t; + +void +score_init (score_t *score); + +void +score_draw (score_t *score, cairo_t *cr); + +#endif